搜尋

首頁  >  問答  >  主體

ios - swift3.0 device token转换问题

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);
PHP中文网PHP中文网2809 天前708

全部回覆(1)我來回復

  • PHP中文网

    PHP中文网2017-04-18 09:52:06

    看樣子,大家都沒人遇到,或太簡單沒人理會。
    不過最後還是找到答案了,
    轉換方法沒錯,只是
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    返回的deviceToken有點問題不知道是蘋果的問題亦不知道是補充一句
    let nsdataStr = NSData.init(data: deviceToken)
    重新實例化一份
    然後執行
    let datastr = nsdataStr.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: "> ", with: "").replacingOccurrences(of: " ", with: "")

         print("deviceToken:\(datastr)")

    就能輸出想要的結果了

    回覆
    0
  • 取消回覆