【发布时间】:2017-01-26 16:06:25
【问题描述】:
如何在 iOS 10 中从新的 xCode 8、Swift 3 获取设备令牌?
这里是注册通知的代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
DispatchQueue.main.async {
let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
return true
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != UIUserNotificationType() {
application.registerForRemoteNotifications()
}
}
在这里我得到了令牌,但我得到了以下错误
Cannot invoke initializer for type 'UnsafePointer<CChar>' with an argument list of type '(UnsafeRawPointer)':
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)
}
谁能帮我解决?
【问题讨论】:
-
您可以在目标 C 中点击此链接,但您可以了解一些想法 stackoverflow.com/questions/39572897/…
标签: push-notification notifications swift3 ios10