Resource request from native iOS Swift applications
To create and configure an iOS native project, first follow the “Configuring a native iOS application with the MobileFirst Platform SDK” tutorial.
If you are developing Swift-based applications, make sure that you follow the additional steps.
MobileFirst applications can access resources using the
WLResourceRequest REST API.
This tutorial explains how to use the
WLResourceRequest API with an HTTP adapter.
Note: The Keychain Sharing capability is mandatory while running iOS apps in the iOS Simulator when using Xcode 8. You need to enable this capability manually before building the Xcode project.
- Access the
WLClientfunctionality by calling the
WLClient.sharedInstancemethod anywhere in your application.
- Initiate the connection to the server by using the
For most actions, you must specify a delegate object, such as a
MyConnectListenerinstance in the following example:
- Make sure that your Bridging Header includes the
WLSwiftBridgingHeader.hfile for access to MobileFirst APIs.
- To specify the delegate object, create a delegate for the
wlConnectWithDelegatemethod and receive the response from the MobileFirst Server instance. Name the class
MyConnectListenerclass, the header file must specify that it implements the
Note: To avoid a compiler error raising that your delegate does not conform to
NSObjectProtocol, make your class a subclass of
WLDelegateprotocol specifies that the class implements the following methods:
- The onSuccess method:
func onSuccess(response: WLResponse!)
- The onFailure method:
func onFailure(response: WLFailResponse!)
onSuccessmethod or the
onFailuremethod of the supplied
MyConnectListenerinstance is called.
In both cases, the response object is passed as an argument.
- Use this object to operate data that is retrieved from the server.
Calling an adapter procedure
WLResourceRequest class handles resource requests to MobileFirst adapters or external resources.
- To call a procedure, create a
WLResourceRequestobject and specify the path to the adapter and the HTTP method.
- Add the required parameters.
paramsparameter name to set an array of parameters.
- For Java adapters or other resources, you can use
setQueryParameterValuefor each parameter.
- Call the procedure by using the
sendWithCompletionHandlermethod. Supply a completion handler to manage the retrieved data.
For more granular management of the retrieved data (such as non-text responses, PDF, etc), you can use the
sendWithDelegatemethod and provide a delegate that conforms to both the
Other signatures, which are not covered in this tutorial, exist for the
sendmethod. Those signatures enable you to set parameters in the body instead of the query, or to handle the response with a delegate instead of a completion handler. See the user documentation to learn more.
Learn more about
WLResourceRequestin the user documentation.
Click to download the MobileFirst project.
Click to download the Native project.
If you use Xcode 7 and iOS 9, read the ATS and Bitcode blog post.
InvokingAdapterProceduresproject contains a MobileFirst native API that you can deploy to your MobileFirst Server instance.
InvokingAdapterProceduresSwift project contains a native iOS Swift application that uses a MobileFirst native API library to communicate with the MobileFirst Server instance.
- Make sure to update the
worklight.plist file in NativeiOSInvoking with the relevant server settings.