1func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
2
3 var error: NSError?
4 let success: Bool = MarketingCloudSDK.sharedInstance().sfmc_configure(&error)
5 if success == true {
6 // The SDK has been fully configured and is ready for use!
7
8 // Turn on logging for debugging. Not recommended for production apps.
9 MarketingCloudSDK.sharedInstance().sfmc_setDebugLoggingEnabled(true)
10
11 // Great place for setting the contact key, tags and attributes since you know the SDK is setup and ready.
12 MarketingCloudSDK.sharedInstance().sfmc_setContactKey("user@example.com")
13 MarketingCloudSDK.sharedInstance().sfmc_addTag("Hiking Supplies")
14 MarketingCloudSDK.sharedInstance().sfmc_setAttributeNamed("FavoriteTeamName", value: "favoriteTeamName")
15
16 DispatchQueue.main.async {
17 if #available(iOS 10.0, *) {
18 // Set the delegate, if needed. Then, ask if we're authorized - the delegate must be set here if used
19 UNUserNotificationCenter.current().delegate = self
20 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {(_ granted: Bool, _ error: Error?) -> Void in
21 if error == nil {
22 if granted == true {
23 // we are authorized to use notifications, request a device token for remote notifications
24 DispatchQueue.main.async {
25 UIApplication.shared.registerForRemoteNotifications()
26 }
27
28 // Support notification categories
29 let exampleAction = UNNotificationAction(identifier: "App", title: "Example", options: [])
30 let appCategory = UNNotificationCategory(identifier: "Example", actions: [exampleAction], intentIdentifiers: [] as? [String] ?? [String](), options: [])
31 let categories = Set<AnyHashable>([appCategory])
32 UNUserNotificationCenter.current().setNotificationCategories(categories as? Set<UNNotificationCategory> ?? Set<UNNotificationCategory>())
33 }
34 }
35 })
36 }
37 else {
38 let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
39 let setting = UIUserNotificationSettings(types: type, categories: nil)
40 UIApplication.shared.registerUserNotificationSettings(setting)
41 UIApplication.shared.registerForRemoteNotifications()
42 }
43 }
44 }
45 else {
46 // MarketingCloudSDK sfmc_configure failed
47 if #available(iOS 10.0, *) {
48 os_log("MarketingCloudSDK sfmc_configure failed with error = %@", error!)
49 } else {
50 // Fallback on earlier versions
51 NSLog("MarketingCloudSDK sfmc_configure failed with error = %@", error!)
52 }
53 }
54
55 return success
56}