Native iOS

Starting your application – Where to begin

We are not attempting to provide a complete how to for getting an iOS application up and running. We are offering some advice in the process that would have been very beneficial to us in retrospect.

Native iOS applications are created using a program made by Apple called xCode. xCode will only run on Apple computers. It is a free download however that you can get from the App Store on your Mac or from developer.apple.com.

The best resource we found for making iOS apps was Stanford’s iTunesU class on iPhone and Ipad app devleopment. The class is taught by Paul Hegarty who really knows his stuff and teaches you how to develop apps the way Apple intended. We originally started with grabbing code snippets online and throwing them into our project. This turned into one ugly mess that didn’t work that well. After listening to Paul’s lectures we saw the big picture and understood how apps were meant to be developed. Spend the time and watch these videos.

It takes a fair amount of leg work to test your iOS apps. You need to have a developer account with Apple that is $99/year. Pariveda has a enterprise account that is managed by Stephen Pillow. Contact him for information on how to be added to the account. Below we have provided below the full documentation on how to set up certificate and provisioning profiles to test your app and send pushes to those devices.

Push Notifications

Before you can test push notifications with your iOS app you must generate an iOS Provisioning Profile which consists of:

  • An App ID and a Push certificate associated with this App ID
  • Developer Certificate(s)
  • Device(s)

This tutorial will discuss the process for obtaining each of these assets (App ID, certificates, etc.) and generating the Provisioning Profile within the context of a developer who is part of a developer team. To get started, an admin user for your team must invite you to the developer team via the Member Center. It may be worthwhile to request that your admin send you an invitation to be a developer with admin privileges as opposed to just a member so you can add your own devices, approve certificates and the like. Regardless, this tutorial will assume you are only a member and will discuss the steps an admin would need to perform on your behalf. That said, once you receive your invitation, follow the instructions to register and become a developer on the team.

[1] Create and request a developer certificate.

Open Keychain Access via Spotlight and navigate to Keychain Access >> Certificate Assistant >> Request a Certificate from a Certificate Authority. Enter your email and name, select the radio button Save to disk and save the certificate request file (CSR).

To request a developer certificate for yourself, login to the iOS Dev Center with your developer account and navigate to the iOS Provisioning Portal. Once in the Provisioning Portal, click on the Certificates section and request a certificate. On the next screen, load up your certificate request then wait for your admin to approve you.

Once you are are approved, download your certificate and open it. It should automatically be loaded into your Keychain of certificates.

[2] Add a device to the development team.

You will need to ask an admin to add a device for you. Alternatively, if you have access to a device that is already added to the team’s list of devices, you may use that instead. To add a device, an admin needs to give the device a name and enter the device’s 40 hex character Device ID from the Devices section in the Provisioning PoÍrtal.

[3] Create an App ID.

You will need to ask an admin to create an App ID from the App IDs section of the Provisioning Portal.

The admin will give the app a description or name of some sort, a Bundle Seed ID and a unique bundle name aka Bundle Identifier. The Bundle Seed ID is automatically generated by Apple. In the words of Apple, “[it] can be utilized to share keychain access between multiple applications you build with a single App ID.” To do Push Notifications, the Bundle Identifier cannot be an asterisk/wildcard. It must be unique so that Push Notifications will arrive at the appropriate application.

Also, you need to make one more certificate so that you can send pushes to your application. This is a Push SSL Certificate that will be used by the application server to interact with APNS. APNS uses this certificate to validate the application server that will send notifications through APNS to your app. You do this by clicking on the App ID that you previously created and then selecting the option to “configure” for development or production. The image below shows our App ID that was already configured for development but has not been configured for production. When you click on the configure button Apple will take you through a process similar to creating a developer certificate. It will ask for a CSR (Certificate Signing Request). You may use the CSR previously generated; however, if you wish to associate the certificate with a different name and email you may want to create a separate request. After submitting this to Apple, they will return you a Push SSL Certificate that you can download and use to send psuhes or give to a 3rd party service like Urban Airship or PushWoosh.

[4] Generate a Provisioning Profile.

An iOS Provisioning Profile acts like a basket for all the developer certificates and devices that you want to associate with a specific App ID. As such, the Provisioning Profile requires that you specify a profile name, one App ID, all developer certificates you want to allow and all devices you will use for development of your app.

Setting up your server (registering with push notification service)

See more about setting up your server.

Registering device for push (get token)

The app you are developing must register with Apple’s APNS. If your app is sucessful in registering your app will then recieve a token from APNS. This token represents your app on a specific device. Think of it as an address that you can send mail to and APNS will deliver a message to your app on the device that the token is assosicated with. Here’s the code you need to make a request to APNS and register for push notifications. This code will typically go into your appDelegate.m in the didFinishLaunchingWithOptions function.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
    // Let the device know we want to receive push notifications
     [[UIApplication sharedApplication]     registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
...
}

Sending the token to the server (could use Urban Airship)

After you send a request to register with APNS, one of two functions in your appDelegate.m will be called in your code. If the request was successful…

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
}

Or if the request was unsuccessful…

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
     NSLog(@"Failed to get token, error: %@", error);
}

How to send a push

See more about sending a push.

How to handle a push

Your app needs to respond to push notifications when they come to the user’s device. When a push comes maybe you want the device to play a sound. Maybe you want the push to trigger your app to go fetch some data from your server and display it on the screen. What you do with a push when it is received will depend a lot on what state your app or the device is in. For example, I made a chat application. When the user is chatting with their friend Sarah and gets a message from sarah, I want the new message from Sarah to be displayed on the screen. But if the user is chatting with Sarah and Tom sends the user a message, we want the app to let the user know they have a message from someone else but not to change the screen to the new message automatically. There are many different scenerios that you app will have for handling push notifications. We have simplified the scenrios into the two most basic levels and describe how to handle them below.

Please note that when a user is actively using your application that pushes sent to the phone will not be displayed in the status bar notification area. Instead the push will invoke a method in your code and you as a developer must decide how to handle it.

The app is closed

This situation includes anytime that your application is not on the screen in front of the user. Or as Apple describes it, your app is not in the foreground. When a push is received and your app is not active, the push notification will be stored in the Notification center. When a user taps on the notification then iOS will call one of two functions in your app. If your app was previously opened but was exited out of and now is “in the background” then iOS will call…

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{

}

This function is aslo called when the app is in the foreground so we need to use an if statement to see if the application was in an active state or in the background. After we have determined that the app was in the background state we can use the NSDictioanry userInfo to access the payload from the push and do whatever logic you need to.

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    if ( application.applicationState == UIApplicationStateActive )
    {
    }
    else
    {
        NSLog(@"Push Notification Payload: %@", userInfo);
        //Perform logic here depending on what was in the push. Maybe you want to switch to a different view controller.
    }
}

Or if the device has been reset or for some other reason the device is not in the background, iOS will call didFinishLaunchingWithOptions and using the NSDictionary launchOptions you can access the payload from the push and do whatever logic you need to.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions != nil)
    {
        NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {
            NSLog(@"Launched from push notification: %@", dictionary);
            //Perform logic here depending on what was in the push. Decide what view controller to launch into or something like that
        }
    }
}

The app is open

This situation occurs when your app is active. Because the app is active the notification will not go into the notification center or display across the top of the screen. The method didReceiveRemoteNotification will be called and we now will put logic in the if statement where the application is active.

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    if ( application.applicationState == UIApplicationStateActive )
    {
        NSLog(@"Push Notification Payload: %@", userInfo);
        //Update the view or move to a different view or whatever you need to do to respond to the push.
    }
    else
    {
        ...
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s