suchen

Heim  >  Fragen und Antworten  >  Hauptteil

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中文网2771 Tage vor691

Antworte allen(1)Ich werde antworten

  • 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)")

    就能输出想要的结果了

    Antwort
    0
  • StornierenAntwort