This WordPress.com site is the bee's knees

Creating a high-powered iOS apps with Bluemix (Part 2)

Part 2: Getting Started with Bluemix & iOS Development

In this section of the tutorial, you will learn how to:

Create a mobile application backend on Bluemix for the iOS development which include the Node.js as a running and pre-built services such as Advanced Mobile Access, Push iOS 8 Notification and Cloudant NoSQL DB in order for you to start assemble your apps faster

Register you existing Xcode project to Bluemix

Install IBM MobileFirst Platform for iOS client SDK and start to leverage the different frameworks provided by the IBM MobileFirst Platform for iOS

Configure the authentication mechanism to access the backend services in the Advanced Mobile Access dashboard

Configure the app in Xcode for Facebook Authentication to access the backend services.

2. Register an existing Xcode project to Bluemix

At this step, we will be registering the existing Xcode project to IBM Bluemix. It is very important that you enter the right Bundle ID and version number of your Xcode project.

2.1 Select Existing Xcode Project2.2 Enter the Bundle ID and Version number of your Xcode project

Note: Bundle ID and Version number of your Xcode project can be found if you bring up Xcode Project > General> Identity

2.3 Initializing SDK with IBM Bluemix App ID and Route
Next, we need to connect the client application with the iOS 8 app that you just created in the Bluemix Dashboard.

To connect your client application with the mobile app in the Bluemix dashboard, we need to copy the Bluemix App ID and Route, which is provided by Bluemix to our project.
From your xc.workspace, Open the PaleoRecipes.plist and update the applicationId, and applicationRoute variables to match the identity provided by the IBM Bluemix.

2.4 Click > DONE
An iOS application is successfully created on Bluemix using Node.js as a runtime and bind with three services, which are:

1. Advance Mobile Access for security to the backend resources and monitoring of the client logging and statistics

2. Push iOS 8 notification, which provides the capability to use iOS 8 features for managing and interacting with notifications in your application

3. Cloudant NoSQL DB, which is a NoSQL database as a service (DBaaS) that can handle many types of database types such as JSON, full-text, and geospatial.

There are two ways that you can set up your client development environment. You can use CocoaPod or Manual Installation. For this tutorial, we will be using the CocoaPod dependency manager.

The CocoaPods dependency manager is a dependency management tool for iOS and OS X development for Swift and Object-C Cocoa project. CocoaPods make adding and removing dependencies for an iOS/OSx developer project a lot easier. In this section you will see that we will be defining our project dependencies in a Podfile and then use the “pod install” command to install the required dependencies for the IBM MobileFirst services.

But before we can use the ‘pod install’ to install our dependencies for IBM MobileFirst services, first we need to make sure that CocoaPods is setup.

3.1 Setup CocoaPods, if you haven’t done so already by running the following command from the terminal

$ sudo gem install cocoapods

$ pod setup

3.2 Create a Podfile. From a terminal, go to your app folder then run

$ touch Podfile

Note: Below is a standard Pod file for using IMF services, we only need to include the resource we need and remove the dependencies that we don’t need

Once all the required dependencies has been installed and configured correctly. We will be able to register the client to the backend using the
IMFClient.sharedInstance().initializeWithBackendRoute(applicationRoute, backendGUID: applicationId)

3.5 In the Build Setting tab, we need to set the Object-C Bridging Header setting under SWIFT compiler to point to the Bridging-Header file that you just created

Tip: To quickly search for what you are looking for type in the word “swift” in the search field

4. Client Registration for Advanced Mobile Access Service

4.1 Client Registration

At this step we will have to configure and select which authentication mechanism we will be using for back-end access. By default it’s currently set to unauthenticated. To configure the app to use a different authentication mechanism such as Facebook, Google or Custom authentication

Note: There are three types of authentication mechanisms that you can use for authentication with the backend resources, which are FaceBook, Google and Custom. For more details on how the iOS app handles the authentication, please see AuthenticationController.swift created by the IBM’s MobileFirst team for more details.

4.1.4 Enter Facebook App ID > Click SAVE

Note: If you don’t have the Facebook App ID. You can find more detail over here (Quick Start for iOS)

4.1.5 You should see a client registration page confirmation similar to my screenshot down below

5. Configure the app in Xcode for the Facebook Authentication for the Advanced Mobile Access

Since we are using Facebook as an identity provider to gain access to the Advanced Mobile Access resources. We will need the Facebook App ID in order to configure both security for Advanced Mobile Access service and the Mobile iOS application. At this step we will be modifying the existing Info.plist file. Please see Quick Start for iOS for more details.

From the Xcode Project >Open Info.plist file

Info.plist should be in the Supporting File Folder inside your Xcode Project

Create a new key for FacebookAppID with String as Type and enter the FacebookAppID

Create a new Key for FacebookAppDisplayName with String as type and enter the FacebookDisplayName

Create a key for URL Type with Array as Type with a dictionary as its only child. Dictionary also needs one array called URL Schema. The child of that array is a string of the facebookID app precedes with the fb in front

Open the AppDelegate.swift and override the application:openURL:sourceApplication:annotation as follows:

Add the following line to the applicationDidBecomeActive This is a function that will restart any task that was paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface

FBAppEvents.activateApp()

Registering the “IMFFacebookAuthenticationHandler to enable iOS application to respond when Advanced Mobile Access requires a Facebook log in. “Advanced Mobile Access provides a delegate by default and logs in to Facebook with read permission to the user’s public profile.”- IBM Documentation

To register the default delegate, add the following SWIFT code to AppDelegate.swift to initiate security for user authentication for the client. This code should be copied to func applicaton(application: UIApplication, didFinishLaunchingWithOptions launchOptions:NSDictionry

// Register a default delegate.

// Logs in to Facebook with read permissions to the user’s public profile.