swift3.0调用注册方法
// 注册获得device Token
UIApplication.shared.registerForRemoteNotifications()
收到回调通知
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let device = deviceToken.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")
print(device)
print("Device Token:\(String(describing: deviceToken))")
}
然而打印的数据一直是
32bytes
Device Token:32 bytes
难道我上面的转换方式写错了吗 为什么不应该是一个正常的字符串的系列号之类
PS:OC写法
NSString *deviceTokenString2 = [[[[deviceTokendescription] stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"方式2:%@", deviceTokenString2);
看樣子,大家都沒人遇到,或太簡單沒人理會。
就能輸出想要的結果了不過最後還是找到答案了,
轉換方法沒錯,只是
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
返回的deviceToken有點問題不知道是蘋果的問題亦不知道是補充一句
let nsdataStr = NSData.init(data: deviceToken)
重新實例化一份
然後執行
let datastr = nsdataStr.description.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: " ", with: "")