Invoking adapter procedures from native iOS applications
improve this page | report issueTo 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.
Initializing WLClient
- Access the
WLClient
functionality by using[WLClient sharedInstance]
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:MyConnectListener *connectListener = [[MyConnectListener alloc] initWithController:self]; [[WLClient sharedInstance] wlConnectWithDelegate:connectListener];
Note: Remember to import WLClient.h and WLDelegate.h in your header file.
You must supply a connection delegate (listener) to the MobileFirst invocation methods.
- Create a delegate to be used in the
wlConnectWithDelegate
method and receive the response from the MobileFirst Server. Name the classMyConnectListener
. The header file must specify that it implements theWLDelegate
protocol.@interface MyConnectListener : NSObject <WLDelegate> { @private ViewController *vc; }
The
WLDelegate
protocol specifies that the class implements the following methods: - The onSuccess method:(WLResponse *)response
- The onFailure method:(WLFailResponse *)response
After
wlConnectWithDelegate
finishes, theonSuccess
method or theonFailure
method of the suppliedMyConnectListener
instance is invoked. In both cases, the response object is sent as an argument. - Use this object to operate data that is retrieved from the server.
-(void)onSuccess:(WLResponse *)response{ NSLog(@"\nConnection Success: %@", response); NSString *resultText = @"Connection success. "; if ([response responseText] != nil){ resultText = [resultText stringByAppendingString:[response responseText]]; } [vc updateView:resultText]; } -(void)onFailure:(WLFailResponse *)response{ NSString *resultText = @"Connection failure. "; if ([response responseText] != nil){ resultText = [resultText stringByAppendingString:[response responseText]]; } [vc updateView:resultText]; }
Invoking an adapter procedure
To invoke a procedure, create a
WLProcedureInvocationData
object and specify the adapter name and the procedure name. Invoke the procedure by using the shared instance of theWLClient
.WLProcedureInvocationData *myInvocationData = [[WLProcedureInvocationData alloc] initWithAdapterName:@"RSSReader" procedureName:@"getStories"]; MyInvokeListener *invokeListener = [[MyInvokeListener alloc] initWithController: self]; [[WLClient sharedInstance] invokeProcedure:myInvocationData withDelegate:invokeListener];
As previously stated, you must supply a delegate object to manage the retrieved data.
Receiving a procedure response
When the procedure invocation is complete, a delegate method of
MyInvokeListener
class instance is called. Any delegate header file must specify that it complies with aWLDelegate
protocol.@interface MyInvokeListener : NSObject <WLDelegate> { @private ViewController *vc; NSString *strResponse; } - (id)initWithController: (ViewController *) mainView;</p> @end
After the procedure invocation 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.-(void)onSuccess:(WLResponse *)response { NSLog(@"Invocation Success: %@", response); NSString *resultText = @"Invocation success. ";</p> if ([response responseText] != nil){ resultText = [resultText stringByAppendingString:[response responseText]]; } [vc updateView:resultText]; } -(void)onFailure:(WLFailResponse *)response{ NSLog(@"Invocation Failure: %@", response); NSString *resultText = @"Invocation failure. ";</p> if ([response responseText] != nil){ resultText = [resultText stringByAppendingString:[response responseText]]; } [vc updateView:resultText]; }
Sample application
The sample contains two projects: - The InvokingAdapterProceduresNativeProject.zip file contains a MobileFirst native API that you can deploy to your MobileFirst server. - The InvokingAdapterProceduresiOSProject.zip file contains a native iOS application that uses a MobileFirst native API library to communicate with the MobileFirst Server.
Make sure to update the worklight.plist file in iOSNativeApp with the relevant server settings.
Click to download the Studio project. Click to download the Native project.