我正在尝试使用CloudKit和Swift来订阅推送通知。这是我的代码:
应用程序委托:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Push
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
if let options: NSDictionary = launchOptions {
let remoteNotification = options.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as? NSDictionary
if let notification = remoteNotification {
self.application(application, didReceiveRemoteNotification: notification as! [NSObject : AnyObject])
}
}
return true
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let ckNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
if ckNotification.notificationType == .Query {
let queryNotification = ckNotification as! CKQueryNotification
let recordID = queryNotification.recordID
let container = CKContainer.defaultContainer()
let privateDatabase = container.privateCloudDatabase
privateDatabase.fetchRecordWithID(recordID!) {newRecord, error in
if error != nil {
print(error)
} else {
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
print(newRecord)
})
}
}
}
}创建:
func addNewRecordsSub() {
let subscription = CKSubscription(recordType: "UserRecords", predicate: predicate, options: .FiresOnRecordCreation)
let notificationInfo = CKNotificationInfo()
notificationInfo.alertBody = "OK!"
notificationInfo.shouldBadge = true
subscription.notificationInfo = notificationInfo
let privateDatabase = container.privateCloudDatabase
privateDatabase.saveSubscription(subscription) { subscription, error in
if error != nil {
print(error)
}
}
}启动订阅出现在CloudKit的仪表板中:

但当我加入新记录时什么都不会发生..。只是什么都没有。我漏掉了什么吗?
发布于 2016-02-19 09:01:37
我又一次试图重新设定环境,现在一切都正常.
https://stackoverflow.com/questions/35455019
复制相似问题