UIUserNotificationType 在 iOS10 Swift 3.0 中被弃用
Posted
技术标签:
【中文标题】UIUserNotificationType 在 iOS10 Swift 3.0 中被弃用【英文标题】:UIUserNotificationType was deprecated in iOS10 Swift 3.0 【发布时间】:2016-09-30 02:33:20 【问题描述】:我在 ios 10 中收到此警告,之前在 iOS 9 上运行良好:-
在 iOS 10 中是否有其他功能可以修复此警告,如果有人对此问题有想法,不胜感激。
【问题讨论】:
请阅读UIUserNotificationSettings
的文档。
@Anbu.Karthik 感谢您提供的信息。 :)
okie 当然,感谢您的努力!
【参考方案1】:
在 iOS10 UIUserNotificationType 已弃用,请使用 UNUserNotificationCenter
别忘了启用这个
适用于 Swift3 样品见this
导入
UserNotifications
框架并在Appdelegate中添加UNUserNotificationCenterDelegate
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!")
使用此委托接收通知
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 Reference
【讨论】:
知道什么时候会放弃对以前框架的支持吗? can you look into it【参考方案2】:// REGISTER FOR PUSH NOTIFICATIONS
if #available(iOS 10.0, *)
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) (granted, error) in
// Enable or disable features based on authorization.
else
// REGISTER FOR PUSH NOTIFICATIONS
let notifTypes:UIUserNotificationType = [.alert, .badge, .sound]
let settings = UIUserNotificationSettings(types: notifTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
application.applicationIconBadgeNumber = 0
【讨论】:
以上是关于UIUserNotificationType 在 iOS10 Swift 3.0 中被弃用的主要内容,如果未能解决你的问题,请参考以下文章
NOIP 2015 & SDOI 2016 Round1 & CTSC 2016 & SDOI2016 Round2游记