Integrating Urban Airship Using Swift

Our recommended approach is to use the Embedded Framework as this includes an automatic bridging header. You should only use the Static Library if you need to target devices lower than iOS 8.

Complete the following steps:

  1. Build Settings and Phases
  2. Including the Urban Airship SDK (click on the relevant library tab and follow instructions)
  3. Enable Background Push
  4. Localization for Built-In Action Buttons (optional)
  5. Create AirshipConfig.plist
  6. If you are using the Embedded Framework:

    • Import AirshipKit in your AppDelegate class:
      import AirshipKit

    If you are using the Static Library: 

    • Create a bridging header
    • Include any required headers in your bridging header:

      #ifndef BridgeHeader_h
      #define BridgeHeader_h
      
      import "UAirship.h"
      import "UAConfig.h"
      import "UAPush.h"
      
      #endif /* BridgeHeader_h */
  7. Starting Urban Airship Services

    The Urban Airship SDK requires only a single entry point in the app delegate, known as takeOff. Inside your application delegate’s application:didFinishLaunchingWithOptions: method, initialize a shared UAirship instance by calling UAirship.takeOff(). This will bootstrap the SDK and look for settings specified in the AirshipConfig.plist file you created earlier. These can also be provided by passing an instance of UAConfig at runtime by calling UAirship.takeOff(config). UAirship.takeOff() must be called on the main thread, in this method, before it returns.

    func application(application: UIApplication, didFinishLaunchingWithOpt
    ions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Populate AirshipConfig.plist with your app's info from
        // https://go.urbanairship.com
        // or set runtime properties here.
        let config: UAConfig = UAConfig.defaultConfig()
        // You can also programmatically override the plist values:
        // config.developmentAppKey = @"YourKey";
        // etc.
    
        // Call takeOff
        UAirship.takeOff(config)
    }
  8. Retrieving your Channel ID The Channel ID is a unique identifier that ties together an application/device pair on iOS devices. The Channel ID is used to target pushes to specific devices using the Urban Airship API. The Channel ID will be logged for all apps. You can always get the channelID with a couple of extra lines to your application:
    let channelID = UAirship.push().channelID
    print("My Application Channel ID: \(channelID)")
    Don’t worry if this value initially comes back as null on your app’s first run. The Channel ID will be created and persisted during registration.
  9. Enabling User Notifications The Urban Airship SDK makes a distinction between “user notifications”, which can be seen by the user, and other forms of push that allow you to send data to your app silently, or in the background. Enabling or disabling user notifications is a preference often best left up to the user, so by default, user notifications are disabled. For testing purposes, enabling notification at startup requires a few extra lines of code to your app delegate.

 To enable user notifications programmatically:

  1. /*
    User notifications will not be enabled until
    userPushNotificationsEnabled is set YES on UAPush. Once enabled,
    the setting will be persisted and the user will be prompted to
    allow notifications. Normally, you should wait for a more
    appropriate time to enable push to increase the likelihood that
    the user will accept notifications.
    */
    
    UAirship.push().userPushNotificationsEnabled = true

A new default behavior was introduced in SDK 6.0.0 ensuring that once user push notifications have been enabled on iOS 8+, settings may only be modified from within the iOS Settings app. Applications should link directly to the application settings.

To disable user push settings on iOS 7 or below, set userPushNotificationsEnabled to NO. If a device is running iOS 8 or higher, we recommend that the application link directly to the application’s system push settings with the UIApplicationOpenSettingsURLString URL constant.

/* Open the system settings app directly to this application (on iOS 8+) */
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)

Related Content: