iOS helloAuthentication Sample Application for Bluemix Mobile Services

The helloAuthentication sample contains a Swift project that you can use to learn more about the Mobile Client Access service.

Use the following steps to configure the helloAuthentication sample for Swift:

  1. Download the helloAuthentication sample
  2. Configure the mobile backend for your helloAuthentication application
  3. Configure the front end in the helloAuthentication sample
  4. Run the iOS app

Before you begin

Before you start, make sure you have:

Download the helloAuthentication sample

Clone the sample from Github with the following command:

git clone https://github.com/ibm-bluemix-mobile-services/bms-samples-swift-helloauthentication

Configure the mobile backend for your helloAuthentication application

Before you can run the helloAuthentication application, you must set up an app on Bluemix. The following procedure shows you how to create a MobileFirst Services Starter application. A Node.js runtime environment is created so that you can provide server-side functions, such as resource URIs and static files. The Cloudant® NoSQL DB, IBM Push Notifications, and Mobile Client Access services are then added to the app.

Create a mobile backend in the Bluemix dashboard:

  1. In the Boilerplates section of the Bluemix catalog, click MobileFirst Services Starter.
  2. Enter a name and host for your mobile backend and click Create.
  3. Click Finish.
  4. Get information about your app.
    After the provisioning process is complete, you will see a a page for your newly provisioned mobile backend. Click the Mobile Options link in top right part of a screen to find your appRoute and appGUID. Keep this screen open in your browser; you will need these parameters in the next steps.

Configure the Mobile Client Access service

  1. In the Mobile Client Access dashboard, go to the Authentication tab to configure your authentication service.
  2. Choose your authentication type (this sample has been configured for Facebook authentication).
  3. Enter the required configuration settings (APP ID for Facebook authentication).

Note: If you have not previously created a Facebook mobile application, follow the instructions on how to Register and Configure an App.

Configure the front end in the helloAuthentication sample

  1. In a terminal, navigate to the bms-samples-swift-helloauthentication directory where the project was cloned.
  2. If the CocoaPods dependency manager is not installed, install it with the following command: sudo gem install cocoapods
  3. If the CocoaPods repository is not configured, run the following command: pod setup
  4. Run the pod install command to download and install the required dependencies.
  5. Open the Xcode workspace: open helloAuthentication-swift.xcworkspace. From now on, open the .xcworkspace file, because it contains all the dependencies and configuration.
  6. Open the AppDelegate.swift file. Add the corresponding ApplicationRoute, ApplicationID and ApplicationRegion in the application didFinishLaunchingWithOptions method:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
       // Override point for customization after application launch.

       BMSClient.sharedInstance.initializeWithBluemixAppRoute("{APPLICATION_ROUTE}", bluemixAppGUID: "{APPLICATION_ID}", bluemixRegion: "your region, choose from BMSClient.REGION_XXX or add your own")
       BMSClient.sharedInstance.authorizationManager = MCAAuthorizationManager.sharedInstance  
       FacebookAuthenticationManager.sharedInstance.register()  
       return true
   }

Set up Facebook authentication

Copy the FacebookAuthenticationManager.swift file from the BMSFacebookAuthentication pod's source folder to your app's folder, and add it to the helloauthentication-swift project.

Update the Info.plist file with your Facebook App information:

  • FacebookAppID (For example 1581349575427190): You can get the App ID from the Facebook developer console.
  • FacebookDisplayName (For example helloAuth): You can get App name from Facebook developer console.

Update URL Types, Item 0, URL Schemes, update Item 0 as follows:

Run the iOS app

Now you can run the iOS application in your iOS emulator or on a physical device.

When you run the application, you will see a single view application with a PING BLUEMIX button. When you click this button the application tests a connection from the client to a protected resource in the backend Bluemix application. Because this is a protected resource, the authentication process will begin. Log in to the authentication service (Facebook in this example). The application will then display if the connection was successful or unsuccessful. In the unsuccessful state, an error is displayed in the application and the output to the Xcode console.

Note: Inside the ViewController, a GET request is made to a protected resource in the Node.js runtime on Bluemix. This code has been provided in the MobileFirst Services Starter boilerplate. The Node.js code provided in this boilerplate must be present in order for the sample to work as expected.

Note: This application runs on the latest version of XCode (V7.0). You might need to modify the application for Application Transport Security (ATS) changes made in iOS 9. For more information, see the following blog entry: Connect Your iOS 9 App to Bluemix.

License

This package contains sample code provided in source code form. The samples are licensed under the under the Apache License, Version 2.0 (the "License"). You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 and may also view the license in the license.txt file within this package. Also see the notices.txt file within this package for additional notices.