Last active
January 30, 2017 17:19
-
-
Save roymckenzie/7e10efa0bc540794d67affa8064793b7 to your computer and use it in GitHub Desktop.
AppDelegate implementation for CloudKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import UserNotifications | |
import CloudKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
func application(_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { | |
// Creates a private database subscription to listen for changes | |
if !CloudKitDatabaseSubscription.private.saved { | |
CloudKitSyncManager.create(databaseSubscription: .private) | |
} | |
// Register for notifications | |
registerForNotifications(application: application) | |
return true | |
} | |
func application(_ application: UIApplication, | |
didReceiveRemoteNotification userInfo: [AnyHashable : Any], | |
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){ | |
let cloudKitNotification = CKNotification(fromRemoteNotificationDictionary: userInfo) | |
guard let subscriptionID = cloudKitNotification.subscriptionID else { | |
print("Received a remote notification for unknown subscriptionID") | |
return | |
} | |
switch subscriptionID { | |
case CloudKitDatabaseSubscription.private.rawValue: | |
CloudKitSyncManager.fetchChanges(for: .private) | |
case CloudKitDatabaseSubscription.public.rawValue: | |
CloudKitSyncManager.fetchChanges(for: .public) | |
default: break | |
// WHATEVER I DON'T EVEN KNOW WHO YOU ARE | |
} | |
} | |
} | |
extension AppDelegate { | |
func registerForNotifications(application: UIApplication) { | |
if #available(iOS 10.0, *) { | |
UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) { success, error in | |
if let error = error { | |
print("Error registering for notifications: \(error)") | |
} | |
if success { | |
print("Successfully registered for notifications") | |
application.registerForRemoteNotifications() | |
} | |
} | |
} else { | |
let settings = UIUserNotificationSettings(types: [.alert], categories: nil) | |
application.registerUserNotificationSettings(settings) | |
application.registerForRemoteNotifications() | |
} | |
} | |
} | |
/// Handles common methods for subscriptions across multiple databases | |
enum CloudKitDatabaseSubscription: String { | |
case `private` | |
case `public` | |
} | |
extension CloudKitDatabaseSubscription { | |
var database: CKDatabase { | |
switch self { | |
case .private: | |
return CKContainer.default().privateCloudDatabase | |
case .public: | |
return CKContainer.default().publicCloudDatabase | |
} | |
} | |
var subscription: CKSubscription { | |
return CKDatabaseSubscription(subscriptionID: subscriptionID) | |
} | |
var subscriptionID: String { | |
return "\(rawValue)SubscriptionIDKey" | |
} | |
var changeToken: CKServerChangeToken? { | |
return UserDefaults.standard.object(forKey: changeTokenKey) as? CKServerChangeToken | |
} | |
var saved: Bool { | |
return UserDefaults.standard.bool(forKey: savedSubscriptionKey) | |
} | |
func set(_ changeToken: CKServerChangeToken?) { | |
UserDefaults.standard.set(changeToken, forKey: changeTokenKey) | |
} | |
func saved(_ saved: Bool) { | |
UserDefaults.standard.set(saved, forKey: savedSubscriptionKey) | |
} | |
private var changeTokenKey: String { | |
return "\(rawValue)DatabaseChangeTokenKey" | |
} | |
private var savedSubscriptionKey: String { | |
return "\(rawValue)SavedSubscriptionKey" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment