iOS end-to-end demonstration
Overview
The purpose of this demonstration is to experience an end-to-end flow:
- A sample application that is pre-bundled with the PMF client SDK is registered and downloaded from the PMF Operations Console.
- A new or provided adapter is deployed to the PMF Operations Console.
- The application logic is changed to make a resource request.
End result:
- Successfully pinging the PMF.
- Successfully retrieving data using an adapter.
Prerequisites:
1. Starting the PMF
Make sure you have created a Persistent Mobile Foundation instance, or
If using the PMF 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 PMF 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.
- Click the New button next to Applications
- Select the iOS platform
- Copy the bundle identifier from XCode and enter 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
-
Click on the Get Starter Code tile and select to download the iOS Objective-C or iOS Swift sample application.
3. Editing application logic
-
Open the Xcode project by double-clicking the .xcworkspace file.
-
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 PMF\n%@", serverURL]; NSLog(@"Did not receive an access token from server: %@", error.description); } else { _titleLabel.text = @"Yay!"; _connectionStatusLabel.text = [NSString stringWithFormat:@"Connected to PMF\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 Mobile Foundation 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 Mobile Foundation 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 PMF Operations Console using the Actions → Deploy adapter action.
Alternatively, click the New button next to Adapters.
-
Select the Actions → Download sample option. Download the “Hello World” Java adapter sample.
If Maven and PMF CLI are not installed, follow the on-screen Set up your development environment instructions.
-
From a Command-line window, navigate to the adapter’s Maven project root folder and run the command:
pmfdev adapter build
-
When the build finishes, deploy it from the PMF Operations Console using the Actions → Deploy adapter action. The adapter can be found in the [adapter]/target folder.
5. Testing the application
- In Xcode, select the mfpclient.plist file and edit the protocol, host and port properties with the correct values for your PMF.
- If using a local PMF, the values are typically http, localhost and 9080.
- If using a remote PMF (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 PMF CLI, then navigate to the project root folder and run the command
pmfdev app register
. If a remote PMF is used, run the commandpmfdev server add
to add the server, followed by for example:pmfdev app register myIBMCloudServer
. - Press the Play button.
Results
- Clicking the Ping PMF button will display Connected to PMF.
- If the application was able to connect to the PMF, a resource request call using the deployed Java adapter will take place.
The adapter response is then printed in the Xcode Console.
Next steps
Learn more on using adapters in applications, and how to integrate additional services such as Push Notifications, using the PMF security framework and more:
- Review the Application development tutorials
- Review the Adapters development tutorials
- Review the Authentication and security tutorials
- Review the Notifications tutorials
- Review All Tutorials