Blog

How to integrate the Plot Plugin into a Swift project

We sometimes get the question whether the Plot plugin also works in iPhone projects written in Swift. Because Swift is largely compatible with Objective-C, the Plot plugin will also work in your Swift project.

There a small number of differences of the original integration guide for iOS. Here is a short guide.

Follow the integration guide

Perform the first five steps and step eight from the iOS integration guide. These steps explain how to configure your project in XCode so all the libraries the Plot Plugin requires are available and all properties are setup.

The other steps are a bit different for Swift.

Bridging header

Because Plot is currently only released as Objective-C project, you have to create a bridging header. Create a new header file with the name: Plot-Bridging-Header.h. Replace the content of the file with:

#import "Plot.h"

This file has to be added to the build settings. Search for the property “Objective-C bridging header”. Set the value to the file path relative to the project root.

Because it isn’t possible to automatically detect whether you are making a debug build or a release build in Swift, you cannot use the class Plot directly. For initialization calls you have use PlotDebug or PlotRelease, where the first will log debug information. For other calls you can use PlotBase.

Delegate

Add the PlotDelegate protocol to your class. For example:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, PlotDelegate {

Configuration

In the AppDelegate call PlotDebug.initializeWithLaunchOptions(launchOptions, delegate:delegate)
in application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?).

An implementation could look like:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  PlotDebug.initializeWithLaunchOptions(launchOptions, delegate:self)

  return true
}

For Plot plugin versions before 1.10.0 you will have to call PlotDebug.initializeWithConfiguration(configuration, launchOptions: launchOptions) instead of PlotDebug.initializeWithLaunchOptions(launchOptions, delegate:delegate).

Last step

The Plot plugin has to handle the notifications. Add the following lines to your AppDelegate.

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
  //Pass the notification to the Plot library to pre process the notification.
  //Don't handle the notifications yourself in this method, because this method isn't always called when a notification is opened.

  PlotBase.handleNotification(notification)
}

When you want custom behaviour when opening the notification, you can implement the plotHandleNotification(notification: UILocalNotification!, data: String!) method.

That’s all

You are now ready to receive your first notification. If you have further questions, please let us know.

Spread the love