Home  >  Q&A  >  body text

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中文网2741 days ago665

reply all(1)I'll reply

  • PHP中文网

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

    It seems that no one has encountered it, or it is too simple and no one cares about it.
    But I finally found the answer,
    The conversion method is correct, it’s just
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    There is something wrong with the deviceToken returned, I don’t know if it’s Apple’s problem or something else
    But I just want to add One sentence
    let nsdataStr = NSData.init(data: deviceToken)
    Re-instantiate a copy
    Then execute
    let datastr = nsdataStr.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: " >", with: "").replacingOccurrences(of: " ", with: "")

         print("deviceToken:\(datastr)")

    You can output the desired results

    reply
    0
  • Cancelreply