我从 apns 开始,我们在应用程序委托(delegate)中有一个方法,我们在其中接收 apns token 。我想将它发送到我的服务器,但前提是它与上次收到的 token 不同(我认为这是正确的做法?)。
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString* newToken = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
NSString* oldToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"lastToken"];
if ([newToken isEqualToString:oldToken]) {
// Nothing to do, token hasn't changed.
}
else {
// 1) Send token up to my server.
// 2) On successful send, overwrite local copy of token.
}
}
我在服务器上用来发送 apns 消息的库最终希望将 token 作为字符串(我正在使用 javapns )。所以我需要在某个时候转换它。我还想将其作为字符串存储在本地。有一个更好的方法吗?
最后,这两个字符串都会被自动释放吗?
谢谢
最佳答案
对于 APNS token ,您应该将它们存储为十六进制,因此:
NSMutableString* binaryString = [NSMutableString stringWithCapacity:deviceToken.length];
unsigned char* bytes = (unsigned char*) [deviceToken bytes];
for (int i = 0 ; i < deviceToken.length ; i++)
[binaryString appendFormat:@"%02x", bytes[i]];
如果您使用的是 ARC,那么它们将被自动发布。
关于ios - 将 apns token 与字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15070095/