我正在使用此代码获取设备令牌它在swift 3中工作但不在swift 4中工作请帮我解决它 .
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print(deviceTokenString)
}
5 回答
假设您已经检查过所有内容都已设置正确,基于您的代码,它似乎应该可以正常工作,您所要做的就是将格式更改为
%02.2hhx
而不是%02X
以获取相应的十六进制字符串 . 因此,你应该得到一个有效的 .作为一种好的做法,您可以将data extension添加到项目中以获取字符串:
用法:
Working Code for getting deviceToken in - iOS 11 or greater, Swift 4
Request user permission
Getting device token
In case of error
获取设备令牌
希望对你有帮助
您可以像这样获取设备令牌: