Integrate your iOS App with the Platform (Swift)

Create an App on the Platform

1. Add your App on the platform by clicking "+ NEW APP" on your XtremePush Dashboard at xtremepush.com

 

 

2. Enter the App Name, upload the App icon, and give a short description of the app. An App key and token have been automatically generated. The App key is used in your iOS project to connect the app and platform. The app token will only be used if you use the external API. Save your settings and copy the app key. Your saved settings should be similar to the following.

 

 

 

Update your info.plist

1. Return to your project in Xcode. In your Info.plist file add "XtremePushApplicationKey" in key field and your application key in value field. You can find the "XtremePushApplicationKey" in the Settings (General Settings) page of your app under 'App Key' as seen above.

 

 

2.  If you plan to use Location Services, make sure to include the entry NSLocationAlwaysUsageDescription in your Info.plist file if you have not already done so. Enter an appropriate value exchange message for why your app uses location services. 

 

 

Upload Push Certs

 Go to your app home and navigate to Settings > Application Keys. On the left hand side select the iOS App radio button and upload your app's iOS push notification certificates for development and production, and click save. If you certificates are password protected be sure to add the correct passwords when uploading them.

If you have not set up push notification certificates before this is covered in our article on APNs Certificates 

 

Initialise the SDK in your App delegate

To enable XtremePush Notifications you must modify your application delegate. This guide is for Swift if you are using Objective-C see the Objective-C Guide. Follow these steps to complete the integration process:

Since the XtremePush iOS SDK is written in Objective-C when working with Swift, you must provide an Objective-C bridging header which will allow you to import the Objective-C Library into your Swift
app.

1. In the same folder as your AppDelegate.swift create a new header file. Go to File > New > File and under iOS - Source choose Header File and click next. This is a bridging header and you can call it PROJECTNAME-Bridging-Header.h.

 

2. In the new file import the XPush.h as you would in Objective-C i.e the file should contain at the end:


#import "XPush.h"

 For Example


//
//  xtremepushShowcase-Bridging-Header.h
//  xtremepushShowcase
//
//  Copyright © 2016 xtremepush. All rights reserved.
//

#ifndef xtremepushShowcase_Bridging_Header_h
#define xtremepushShowcase_Bridging_Header_h


#endif /* xtremepushShowcase_Bridging_Header_h */

//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "XPush.h"

3. Now you are ready to use the XtremePush SDK in your Swift app. The Objective-C functionality will be available in any Swift file within that target automatically, without any import statements To initialise XtremePush you must modify your Application Delegate. Inside application didFinishLaunchingWithOptions add the following code to set up XtremePush:



func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// Override point for customisation after application launch.

//Start - Initialise XPush

var intTypes: NSInteger?

if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8.0 {
    let types = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
    intTypes = NSInteger(types.rawValue)
}else {
    let types = UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge
    intTypes = NSInteger(types.rawValue) 
}

XPush.registerForRemoteNotificationTypes(intTypes!)
XPush.applicationDidFinishLaunchingWithOptions(launchOptions)

//End - Initialise XPush

Development/Debug buildsIf you test using development builds then you should turn on Sandbox Mode as Apple uses a different gateway for builds compiled with a development mobile provisioning profile. if you only do enterprise builds you won't need this. Set sandbox mode in a statement that will only run for development  builds as it should not be turned on in production. You can also turn on debug logs for the SDK in this way:

XPush.registerForRemoteNotificationTypes(intTypes!)
// Only runs for development builds sets push to use sandbox mode and turns on debug logs     
#if DEBUG    
XPush.setSandboxModeEnabled(true)
XPush.setShouldShowDebugLogs(true)
#endif
XPush.applicationDidFinishLaunchingWithOptions(launchOptions)
 


4. Finally add the the iOS remote notifications handling methods in the Application Delegate, and place the corresponding XtremePush method call in each of these as shown.


//Notification Handling Methods
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        XPush.applicationDidRegisterForRemoteNotificationsWithDeviceToken(deviceToken)
    }
    
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        XPush.applicationDidFailToRegisterForRemoteNotificationsWithError(error)
    }
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        XPush.applicationDidReceiveRemoteNotification(userInfo)
    }
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        XPush.applicationDidReceiveLocalNotification(notification)
    }

Other API calls

Now that the XtremePush SDK has been successfully linked to your Swift App you will be able to use the remaining API calls throughout your app.

API calls map from Objective-C to Swift very simply for example the Objective-C tagging method hitTag:



// You might for example want to tag a button press
[XPush hitTag: @"Checkout_Button_Pressed"];

can be used in Swift as follows:


// You might for example want to tag a button press 
XPush.hitTag("Checkout_Button_Pressed")
Have more questions? Submit a request

0 Comments

Article is closed for comments.