Invoking adapter procedures from native iOS Swift applications
improve this page | report issueOverview
To create and configure an iOS native project, first follow the "Creating your first Native iOS MobileFirst application" tutorial.
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.Make sure that you follow the extra steps for Swift-based applications.
Initializing WLClient
- Access the
WLClient
functionality by calling theWLClient.sharedInstance
method anywhere in your application. - Initiate the connection to the server by using the
wlConnectWithDelegate
method.
For most actions, you must specify a delegate object, such as aMyConnectListener
instance in the following example:let connectListener = MyConnectListener(vc: self) WLClient.sharedInstance().wlConnectWithDelegate(connectListener)
- Make sure that your Bridging Header includes WLSwiftBridgingHeader.h for access to MobileFirst APIs.
- To specify the delegate object, create a delegate for the
wlConnectWithDelegate
method and receive the response from the MobileFirst Server instance. Name the classMyConnectListener
.For yourMyConnectListener
class, the header file must specify that it implements theWLDelegate
protocol.Note: To avoid a compiler error raising that your delegate does not conform to
NSObjectProtocol
, make your class a subclass ofNSObject
.class MyConnectListener: NSObject, WLDelegate{ //... }
The
WLDelegate
protocol specifies that the class implements the following methods:
- The onSuccess method:func onSuccess(response: WLResponse!)
- The onFailure method:func onFailure(response: WLFailResponse!)
After
wlConnectWithDelegate
finishes, theonSuccess
method or theonFailure
method of the suppliedMyConnectListener
instance is called.
In both cases, the response object is sent as an argument. - Use this object to operate data that is retrieved from the server.
func onSuccess(response: WLResponse!) { var resultText = "Connection success. " if(response != nil){ resultText += response.responseText } self.vc.updateView(resultText) } func onFailure(response: WLFailResponse!) { var resultText = "Connection failure. " if(response != nil){ resultText += response.errorMsg } self.vc.updateView(resultText) }
Calling an adapter procedure
- To call a procedure, create a
WLProcedureInvocationData
object and specify the adapter name and the procedure name.let invocationData = WLProcedureInvocationData(adapterName: "RSSReader", procedureName: "getStories")
- Call the procedure by using the shared instance of
WLClient
. As previously stated, supply a delegate object to manage the retrieved data.let invokeListener = MyInvokeListener(vc: self) WLClient.sharedInstance().invokeProcedure(invocationData, withDelegate: invokeListener)
Receiving a procedure response
When the procedure call is complete, a delegate method of the
MyInvokeListener
instance is called.
Any delegate header file must specify that it complies with aWLDelegate
protocol.class MyInvokeListener: NSObject, WLDelegate{ }
After the procedure call finishes, the
onSuccess
method or theonFailure
method of the suppliedMyInvokeListener
instance is called.
In both cases, a response object is sent as an argument. - Use this object to operate data that is retrieved from the server.
func onSuccess(response: WLResponse!) { var resultText = "Invocation success. " if(response != nil){ resultText += response.responseText } self.vc.updateView(resultText) } func onFailure(response: WLFailResponse!) { var resultText = "Invocation failure. " if(response != nil){ resultText += response.errorMsg } self.vc.updateView(resultText) }
Sample application
The attached sample contains two projects:
- The InvokingAdapterProceduresNativeProject.zip file contains a MobileFirst native API that you can deploy to your MobileFirst server.
- The InvokingAdapterProceduresSwiftProject.zip file 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 SwiftNativeApp with the relevant server settings.
Click to download the Studio project.
Click to download the Native project.
