事實證明,你不能簡單地打電話UIApplication.sharedApplication().registerUserNotificationSettings
並添加一個新的行動 - 這樣做將註冊只有單一的,新的行動(並吹走你所有的其他人)。
這裏就是我最後做的application: didFinishLaunchingWithOptions
:
注:我用的count < 2
邏輯檢查,因爲在以前的版本我的應用程序,我只建立在目標類別(「REPLY_CATEGORY」一個行動)。
// This code ensures that iOS 9 Users (especially iOS 8 ==> iOS 9 upgraders) will be able to see
// iOS 9's new text input 'behavior', available for notifications quick-actions
if #available(iOS 9, *) {
if NSUserDefaults.standardUserDefaults().boolForKey("asked_for_notification_permission") {
let settings: UIUserNotificationSettings = UIApplication.sharedApplication().currentUserNotificationSettings()!
for category in settings.categories! {
let cat: UIUserNotificationCategory = category
if cat.identifier == "REPLY_CATEGORY" {
if cat.actionsForContext(UIUserNotificationActionContext.Default)!.count < 2 {
print("! aD: iOS 9 User has less than the expected reply actions")
// Re-build entire categories/actions here, including
// the new action with .behavior = .TextInput
// THEN, simply re-register:
let notificationCategories = NSSet(array: allCategories)
let settings: UIUserNotificationType = [.Sound, .Alert, .Badge]
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: settings, categories: (notificationCategories as! Set<UIUserNotificationCategory>)))
}
}
}
}
}
來源
2015-09-07 18:54:52
Dan
代碼難以理解。如果您有意見,請將它們格式化爲評論。 – matt