Growthbeat SDK with Swift - growthbeat/Growthbeat-SDK-iOS-Starter-Project GitHub Wiki

Usage

1. Add the Growthbeat SDK to your Xcode Project

CocoaPods

$ pods install

2. Configure Xcode Project

  1. In Xcode create growthbeat.plist and right-click and choose "Open As > Source Code".
  2. Add YOUR_APLICATION_ID and YOUR_CREDENTIAL_ID like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>GrowthbeatAppID</key>
        <string>{YOUR_APLICATION_ID}</string>
        <key>GrowthbeatCredentialID</key>
        <string>{YOUR_CREDENTIAL_ID}</string>
    </dict>
</plist>

3. Create Bridging-Header.h

Growthbeat SDK is written with Objective-C so You need to create <project name>-Bridging-Header.h to use file written in Objective-C in Swift Project. New File ... -> Header File -> <project name>-Bridging-Header.h -> Create and then a file will be generated.

4. Add #import <Growthbeat/Growthbeat.h>

Add #import <Growthbeat/Growthbeat.h> in <project name>-Bridging-Header.h:

#import <Growthbeat/Growthbeat.h>

5. Configure Bridging-Header.h

And you need to configure to import <project name>-Brighing-Header.h when compiling Swift project. Target -> Build Setting -> Swift Compiler - Code Generation -> Objective-C Bridging Header. You add it below:

$(SRCROOT)/$(PROJECT)/$(PROJECT)-Bridging-Header.h

6. Initialize Growthbeat SDK

Growthbeat.sharedInstance().initializeWithApplicationId("APPLICATION_TOKEN", credentialId: "YOUR_CREDENTIALID");

7. Add Growth Push const

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    #if DEBUG
    let kGPEnvironment:GPEnvironment = GPEnvironment.Development
    #else
    let kGPEnvironment:GPEnvironment = GPEnvironment.Production
    #endif

8 . Debug

Build Settings -> Swift Compiler - Custome Flag Other Swift Flags:

-D DEBUG

9. Get device token

GrowthPush.sharedInstance().requestDeviceTokenWithEnvironment(kGPEnvironment)

10. Send device token

func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
    GrowthPush.sharedInstance().setDeviceToken(deviceToken)
}

11. Result

After all minimum AppDelegate.swift will be like below:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
+   #if DEBUG
+   let kGPEnvironment:GPEnvironment = GPEnvironment.Development
+   #else
+   let kGPEnvironment:GPEnvironment = GPEnvironment.Production
+   #endif

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
+       Growthbeat.sharedInstance().initializeWithApplicationId("APPLICATION_TOKEN", credentialId: "YOUR_CREDENTIALID");
+       GrowthPush.sharedInstance().requestDeviceTokenWithEnvironment(kGPEnvironment)
        return true
    }

+    func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
+        GrowthPush.sharedInstance().setDeviceToken(deviceToken)
+    }
⚠️ **GitHub.com Fallback** ⚠️