For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.

Create some initial code in iOS

A simple startup process for iOS is described here with short samples for both Objective C and Swift. Before trying the sample, make sure you have imported the frameworks and added the necessary imports.

Accessing a server resource

In the ViewController.m file, after the ViewController loads (in the viewdidLoad method of the ViewController), we can create a resource request without first creating a client.

Objective C

((void)viewDidLoad{ [super viewDidLoad]; NSURL*url=[NSURL URLWithString:@"/adapters/javaAdapter/users/world"]; WLResourceRequest*request=[WLResourceRequest requestWithURL:url method:WLHttpMethodGet]; [request sendWithCompletionHandler:^(WLResponse*response,NSError*error){ if(error!=nil){ NSLog(@"Failure: %@",error.description); } else if(response!=nil){ // Will print "Hello world" in the Xcode Console. NSLog(@"Success: %@",response.responseText); } } ]; }


override func viewDidLoad() { super.viewDidLoad() let url = NSURL(string: "/adapters/javaAdapter/users/world") let request = WLResourceRequest(URL: url, method: WLHttpMethodGet) request.sendWithCompletionHandler { (WLResponse response, NSError error) -> Void in if (error != nil){ NSLog("Failure: " + error.description) } else if (response != nil){ NSLog("Success: " + response.responseText) } } }

Testing the server connection without a server resource

If you have not created any server resources and we want to test the server connection, we can test the token access. If you have not created any security checks, the access token should be available. As in the previous example, in the ViewController.m file, after the ViewController loads, we can request token access without any scope.

Objective C

(void)viewDidLoad{ [super viewDidLoad]; [[WLAuthorizationManager sharedInstance] obtainAccessTokenForScope: @"" withCompletionHandler:^(AccessToken *accessToken, NSError *error) { if (error != nil){ NSLog(@"Failure: %@",error.description); } else if (accessToken != nil){ NSLog(@"Success: %@",accessToken.value); } }]; }


override func viewDidLoad() { super.viewDidLoad() WLAuthorizationManager.sharedInstance().obtainAccessTokenForScope(nil) { (token, error) -> Void in if (error != nil) { print("Did not recieve an access token from server: " + error.description) } else { print("Received access token value: " + token.value) } } }

For more information about logging, see Logger SDK.

For more information about adapters, see Client access to adapters.

Parent topic: Developing native applications for iOS in Xcode