Starting the SDK
Here, we'll show you how to start the SDK within your app.
[warning] Use the Workspace!
As with all CocoaPod projects, to open your project in Xcode, use the .xcworkspace file, not the .xcodeproj file.
1. New user setup
Set doesn't require any details about your users. However, we require that you provide a unique ID for your user at startup time and we recommend that you launch the SDK after you have prompted the user for any data permissions you plan to use with the SDK. Set requires Notifications permissions and offers some enhanced APIs when additional Background Location and/or Motion permissions are available. See User Permissions section for more details.
2. Update your app delegate
Your app needs to be configured to receive silent push notifications from your Set Dashboard. This is handled in your AppDelegate.swift. We include three new methods at the end of our our AppDelegate class.
The first method registers each new device with your Set Dashboard (the second handles failure).
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
SetSDK.registeredForPushNotifications(withToken: deviceToken)
}
The second handles failure events during registration.
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
SetSDK.failedToRegisterForPushNotifications(withError: error)
}
The next method lets the Set SDK know that new information has come from your dashboard for the user.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
SetSDK.receivedRemoteNotification(userInfo: userInfo, fetchCompletionHandler: completionHandler)
}
You can add these two helper functions to the end of your AppDelegate class. They allow you to quickly request notification permissions from your user.
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}
func registerForPushNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
guard granted else { return }
self.getNotificationSettings()
}
}
Finally, you can update your default application method inside of AppDelegate to request notification permissions using our helper function registerForPushNotifications
above.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
registerForPushNotifications()
}
3. Import and launch Set
Create a Configuration
with your client credentials, then call launch on the shared instance (See launch for more about this method). For more information on the Configuration options, see Configuration Type in the reference section. Using the MVVM app design, we typically embed these methods inside of the RootViewModel
but it will work just as well to add them to your initial ViewController.
Resources
import Set
let configuration = Configuration(clientId: clientId, clientSecret: clientSecret, userId: userId)
SetSDK.instance.launch(with: configuration) { error in
if let error = error {
// Oops, error
} else {
// Set is ready and learning!
}
}
You can also wrap it into a nice clean function as follows.
private func launchSetSDK(withUserId id: String) {
let setConfiguration = Configuration(clientId: setClientId, clientSecret: setClientSecret, userId: id)
SetSDK.instance.launch(with: setConfiguration) { [weak self] possibleError in
guard let _ = self else { return }
if let error = possibleError {
return
}
}
}
[success] Let's do this!
All the house-keeping is behind you! Check out the Context APIs Overview section for next steps.
Get in touch
- If you need help, shoot us a message at [email protected] or ping us on our Slack community.
- If found a bug, or have a feature request, add an Issue on the SetSDK Repo.
- Follow us on Twitter for product updates: @everyset