ios - swift3.0 device token转换问题
PHP中文网
PHP中文网 2017-04-18 09:50:06
0
1
654

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中文网

认证高级PHP讲师

Antworte allen(1)
Ty80

看样子,大家都没人遇到,或者太简单没人理会。
不过最后还是找到答案了,
转换方法没错,只是
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)")

就能输出想要的结果了

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!