在iOS10 Swift 3.0中不推荐使用UIUserNotificationType [英] UIUserNotificationType was deprecated in iOS10 Swift 3.0
问题描述
我在iOS 10中收到此警告,之前在iOS 9上工作正常: -
I am getting this warning in iOS 10 which previously is working fine on iOS 9 :-
在iOS 10中是否有其他功能可以修复此警告,如果有人对这个问题有所了解,我们表示赞赏。
Is there another function to fix this warning in iOS 10, appreciated if anyone would have idea for this issue.
推荐答案
in iOS10 UIUserNotificationType 已弃用,请使用 UNUserNotificationCenter
in iOS10 UIUserNotificationType has deprecated , use UNUserNotificationCenter
别忘了启用此功能
for Swift3
样本见这个
导入
UserNotification s
框架并在Appdelegate中添加UNUserNotificationCenterDelegate
import the
UserNotifications
framework and add theUNUserNotificationCenterDelegate
in Appdelegate
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//create the notificationCenter
let center = UNUserNotificationCenter.current()
center.delegate = self
// set the type as sound or badge
center.requestAuthorization(options: [.sound,.alert,.badge]) { (granted, error) in
// Enable or disable features based on authorization
}
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let chars = UnsafePointer<CChar>((deviceToken as NSData).bytes)
var token = ""
for i in 0..<deviceToken.count {
token += String(format: "%02.2hhx", arguments: [chars[i]])
}
print("Registration succeeded!")
print("Token: ", token)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Registration failed!")
}
使用此代表接收通知
receive the Notifications using this delegates
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground")
// custom code to handle push while app is in the foreground
print("\(notification.request.content.userInfo)")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Handle push from background or closed")
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
print("\(response.notification.request.content.userInfo)")
}
了解Apple API中可以看到的更多信息参考
for more Information you can see in Apple API Reference
这篇关于在iOS10 Swift 3.0中不推荐使用UIUserNotificationType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!