Iphone device token - NSData or NSString

前端 未结 6 1552
走了就别回头了
走了就别回头了 2020-12-13 13:38

I am receiving iPhone device token in the form of NSData object. When I tested my notifications script function, I have only copied that object from log and th

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 14:39

    I found this solution better as iOS can change the usage of description in future versions, so using description property on data can be unreliable in future. We can directly use this by creating hex Token from the data token bytes.

     - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
     const unsigned *tokenBytes = [deviceToken bytes];
     NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                      ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                      ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                      ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
     [[MyModel sharedModel] setApnsToken:hexToken];
    

    }

    We can also store the device token in our NSUserdefaults and use it later to send it out to our server.

提交回复
热议问题