我得到在iOS的10此警告這在以前是工作的罰款在iOS 9: - UIUserNotificationType在iOS10斯威夫特棄用3.0
是否有另一種功能來修復iOS的10此警告,若有人要讚賞有這個問題的想法。
我得到在iOS的10此警告這在以前是工作的罰款在iOS 9: - UIUserNotificationType在iOS10斯威夫特棄用3.0
是否有另一種功能來修復iOS的10此警告,若有人要讚賞有這個問題的想法。
在iOS10UIUserNotificationType已經過時,用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)")
}
更多的信息,你可以在蘋果的API看到的通知Reference
它像一個魅力工作。 :) 謝謝 ! – aznelite89
支持前一個框架的任何想法都將被刪除? – justColbs
請閱讀'UIUserNotificationSettings'的文檔。 – rmaddy
@ Anbu.Karthik感謝您的信息。 :) – aznelite89
okie當然,謝謝你的努力! – aznelite89