swift Swift - CloudKit订阅 - 5

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift - CloudKit订阅 - 5相关的知识,希望对你有一定的参考价值。

See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/SubscribingtoRecordChanges/SubscribingtoRecordChanges.html

01. Save Subscriptions to the Database

In your code, create a subscription object specifying the record type, predicate, and types of changes you 
want to be notified about. Then save the subscription object to the database.

To create and save a subscription

A. Create a predicate object.
For example, subscribe to artwork from an artist (where the artist field in the Artwork record type 
is a Reference type).

var artistRecordID = CKRecordID(recordName: "Mei Chen")
var predicate = NSPredicate(format: "artist = %@", artistRecordID)

Note: Possible values for the right-hand expression in the predicate format string parameter include
CKRecord, CKRecordID, and CKReference objects. If you know the record name, you can create a record 
ID containing just the record name.

B. Create a subscription specifying the record type, predicate, and notification options.

var subscription = CKSubscription(recordType: "Artwork", predicate: predicate, options: .firesOnRecordCreation)

The possible values for the options parameter are: CKSubscriptionOptionsFiresOnRecordCreation, 
CKSubscriptionOptionsFiresOnRecordDeletion, CKSubscriptionOptionsFiresOnRecordUpdate, and 
CKSubscriptionOptionsFiresOnce. Because the options parameter is a bitmask, you can subscribe 
to any combination of the type of changes. For example, you can pass 
CKSubscriptionOptionsFiresOnRecordCreation | CKSubscriptionOptionsFiresOnRecordUpdate as the 
options: parameter to receive notification of all new data.

C. Create a CloudKit notification object.

var notificationInfo = CKNotificationInfo()
notificationInfo.alertLocalizationKey = "New artwork by your favorite artist."
notificationInfo.shouldBadge = true

To display a localized string to the user, set the notification’s alertLocalizationKey property 
(not the alertBody property).

D. Set the subscription’s notification object to the new CloudKit notification object.

subscription.notificationInfo = notificationInfo

E. Save the subscription to the database.

var publicDatabase: CKDatabase? = CKContainer.default().publicCloudDatabase
publicDatabase
CKSubscription * subscription, NSError
error
do {
    if error != nil {
        // insert error handling
    }
}

02. Register for Push Notifications

Saving subscriptions to the database doesn’t automatically configure your app to receive notifications 
when a subscription fires. CloudKit uses the Apple Push Notification service (APNs) to send subscription 
notifications to your app, so your app needs to register for push notifications to receive them.

For iOS and tvOS apps, add this code to the application:didFinishLaunchingWithOptions: protocol method 
to register for push notifications:

// Register for push notifications

var notificationSettings = UIUserNotificationSettings(types: .alert, categories: nil)
application.registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()

For Mac apps, implement the applicationDidFinishLaunching: protocol method to register for push 
notifications.

Optionally implement the application:didRegisterForRemoteNotificationsWithDeviceToken: and 
application:didFailToRegisterForRemoteNotificationsWithError: methods to take the appropriate action 
when the app successfully or unsuccessfully registers for push notifications.

Note: You don’t need to enable push notifications for the app’s explicit App ID in your developer
account to receive subscription notifications. Xcode automatically adds the APNs entitlement to your
entitlement file when you enable CloudKit.

03.Handle Push Notifications in Code

Next, implement the application:didReceiveRemoteNotification: method to process subscription 
notifications when they arrive. For iOS and tvOS apps, implement the UIApplicationDelegate 
protocol method and for Mac apps, implement the NSApplicationDelegate protocol method. 
For example, implement this method to update views when records matching your predicate are 
created, updated, or deleted.


A. Add the application:didReceiveRemoteNotification: protocol method to the app’s delegate.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
}

B. In the application:didReceiveRemoteNotification: method, convert the userInfo parameter to
aCKNotification object.

var cloudKitNotification = CKNotification(fromRemoteNotificationDictionary: userInfo)

C. Get the body of the notification.

var alertBody: String? = cloudKitNotification.alertBody

D. Get the new or modified record from the CKQueryNotification object.

if cloudKitNotification.notificationType == .query {
    var recordID: CKRecordID? = (cloudKitNotification as? CKQueryNotification)?.recordID
}

E. Update views or notify the user according to the record changes.



以上是关于swift Swift - CloudKit订阅 - 5的主要内容,如果未能解决你的问题,请参考以下文章

swift Swift - CloudKit - 获取用户ID

swift Swift - CloudKit - 推送通知

swift Swift - CloudKit共享

swift Swift - CloudKit - 维护本地缓存

swift Swift - CloudKit - 部署架构

swift Swift - CloudKit订阅 - 5