iOS end-to-end demonstration

improve this page | report issue

Overview

The purpose of this demonstration is to experience an end-to-end flow:

  1. A sample application that is pre-bundled with the MobileFirst client SDK is registered and downloaded from the MobileFirst Operations Console.
  2. A new or provided adapter is deployed to the MobileFirst Operations Console.
  3. The application logic is changed to make a resource request.

End result:

  • Successfully pinging the MobileFirst Server.
  • Successfully retrieving data using an adapter.

Prerequisites:

  • Xcode
  • Optional. MobileFirst CLI (download)
  • Optional. Stand-alone MobileFirst Server (download)

1. Starting the MobileFirst Server

Make sure you have created a Mobile Foundation instance, or
If using the MobileFirst Developer Kit, navigate to the server’s folder and run the command: ./run.sh in Mac and Linux or run.cmd in Windows.

2. Creating an application

In a browser window, open the MobileFirst Operations Console by loading the URL: http://your-server-host:server-port/mfpconsole. If running locally, use: http://localhost:9080/mfpconsole. The username/password are admin/admin.

  1. Click the New button next to Applications
    • Select the iOS platform
    • Enter com.ibm.mfpstarteriosobjectivec or com.ibm.mfpstarteriosswift as the application identifier (depending on the application scaffold you will download in the next step)
    • Enter 1.0 as the version value
    • Click on Register application

    Register an application

  2. Click on the Get Starter Code tile and select to download the iOS Objective-C or iOS Swift sample application.

    Download sample application

3. Editing application logic

  1. Open the Xcode project by double-clicking the .xcworkspace file.

  2. Select the [project-root]/ViewController.m/swift file and paste the following code snippet, replacing the existing getAccessToken() function:

    In Objective-C:

    - (IBAction)getAccessToken:(id)sender {
    _testServerButton.enabled = NO;
    NSURL *serverURL = [[WLClient sharedInstance] serverUrl];
    _connectionStatusLabel.text = [NSString stringWithFormat:@"Connecting to server...\n%@", serverURL];
    
    NSLog(@"Testing Server Connection");
    [[WLAuthorizationManager sharedInstance] obtainAccessTokenForScope:@"" withCompletionHandler:^(AccessToken *token, NSError *error) {
         if (error != nil) {
             _titleLabel.text = @"Bummer...";
             _connectionStatusLabel.text = [NSString stringWithFormat:@"Failed to connect to MobileFirst Server\n%@", serverURL];
             NSLog(@"Did not receive an access token from server: %@", error.description);
         } else {
             _titleLabel.text = @"Yay!";
             _connectionStatusLabel.text = [NSString stringWithFormat:@"Connected to MobileFirst Server\n%@", serverURL];
             NSLog(@"Received the following access token value: %@", token.value);
    
             NSURL* url = [NSURL URLWithString:@"/adapters/javaAdapter/resource/greet/"];
             WLResourceRequest* request = [WLResourceRequest requestWithURL:url method:WLHttpMethodGet];
    
             [request setQueryParameterValue:@"world" forName:@"name"];
             [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);
                 }
             }];
         }
    
         _testServerButton.enabled = YES;
     }];
    }
    

    In Swift:

    @IBAction func getAccessToken(sender: AnyObject) {
         self.testServerButton.isEnabled = false
         let serverURL = WLClientSwift.sharedInstance().serverUrl();
         connectionStatusLabel.text = "Connecting to server...\n\(String(describing: serverURL))"
         print("Testing Server Connection")
         WLAuthorizationManagerSwift.sharedInstance().obtainAccessToken(forScope: nil) { (token,error) -> Void in
             if (error != nil) {
                 self.titleLabel.text = "Bummer..."
                 self.connectionStatusLabel.text = "Failed to connect to MobileFirst Server\n\(String(describing: serverURL))"
                 print("Did not receive an access token from server: " + error.debugDescription)
             } else {
                 self.titleLabel.text = "Yay!"
                 self.connectionStatusLabel.text = "Connected to MobileFirst Server\n\(String(describing: serverURL))"
                 print("Received the following access token value: " + (token?.value)!);
                 let url = URL(string: "/adapters/javaAdapter/resource/greet/");
                 let request = WLResourceRequestSwift(url: url!, method: WLResourceRequestSwift.WLHttpMethodGet);
                 request.setQueryParameterValue(parameterValue: "world", forName: "name");
                 request.send(onCompletion: { (response, error) in
                     if (error != nil){
                         print("Failure: " , error!);
                     }
                     else if (response != nil){
                         print("Success: " + response!.responseText);
                     }
                 })
             }
             self.testServerButton.isEnabled = true
         }
     }
    

4. Deploy an adapter

Download this prepared .adapter artifact and deploy it from the MobileFirst Operations Console using the Actions → Deploy adapter action.

Alternatively, click the New button next to Adapters.

  1. Select the Actions → Download sample option. Download the “Hello World” Java adapter sample.

    If Maven and MobileFirst CLI are not installed, follow the on-screen Set up your development environment instructions.

  2. From a Command-line window, navigate to the adapter’s Maven project root folder and run the command:

    mfpdev adapter build
    
  3. When the build finishes, deploy it from the MobileFirst Operations Console using the Actions → Deploy adapter action. The adapter can be found in the [adapter]/target folder.

    Deploy an adapter

sample app

5. Testing the application

  1. In Xcode, select the mfpclient.plist file and edit the protocol, host and port properties with the correct values for your MobileFirst Server.
    • If using a local MobileFirst Server, the values are typically http, localhost and 9080.
    • If using a remote MobileFirst Server (on IBM Cloud), the values are typically https, your-server-address and 443.
    • If using a Kubernetes cluster on IBM Cloud Private and if the deployment is of type NodePort, the value of the port would typically be the NodePort exposed by the service in Kubernetes cluster.

    Alternatively, if you have installed the MobileFirst CLI, then navigate to the project root folder and run the command mfpdev app register. If a remote MobileFirst Server is used, run the command mfpdev server add to add the server, followed by for example: mfpdev app register myIBMCloudServer.

  2. Press the Play button.


Results

  • Clicking the Ping MobileFirst Server button will display Connected to MobileFirst Server.
  • If the application was able to connect to the MobileFirst Server, a resource request call using the deployed Java adapter will take place.

The adapter response is then printed in the Xcode Console.

Image of application that successfully called a resource from the MobileFirst Server

Next steps

Learn more on using adapters in applications, and how to integrate additional services such as Push Notifications, using the MobileFirst security framework and more:

Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on June 01, 2020