保存 DeviceToken 供以后在 Apple 推送通知服务中使用
在我的 iPhone 应用程序中,我从 Apple 获取设备令牌,我在委托文件内分配一个公共属性,如下所示:
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
self.dToken = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
}
dToken 属性声明如下:
NSString *dToken;
@property (nonatomic,retain) NSString *dToken;
但是当我尝试从另一个文件检索设备令牌时,我得到空值。
+(NSString *) getDeviceToken
{
NSString *deviceToken = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] dToken];
NSLog(@" getDeviceToken = %@",deviceToken); // This prints NULL
return deviceToken;
}
我做错了什么?
In my iPhone app I am getting the device token from Apple which I am assigning a public property inside the Delegate file as shown below:
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
self.dToken = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
}
The dToken property is declared as shown below:
NSString *dToken;
@property (nonatomic,retain) NSString *dToken;
But when I try to retrieve the device token from another file I get the null value.
+(NSString *) getDeviceToken
{
NSString *deviceToken = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] dToken];
NSLog(@" getDeviceToken = %@",deviceToken); // This prints NULL
return deviceToken;
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从将 NSData 序列化为十六进制字符串,这是一个更好的方法。更长,但如果 Apple 更改 NSData 发出调试器描述的方式,您的代码将不会过时。
扩展 NSData 如下:
From the discussion at Best way to serialize an NSData into a hexadeximal string, here is a better way to do it. Is longer, but your code will be future-proof if Apple changes the way NSData emit debugger descriptions.
Extend NSData as follows:
我知道这是一个老问题,并且可能是从那时起出现的新信息,但我想向所有声称使用描述方法是一个非常糟糕的主意的人指出一些事情。在大多数情况下,你是完全正确的。 description 属性通常仅用于调试,但对于 NSData 类,它被专门定义为返回接收者内容的十六进制表示形式,这正是这里所需要的。由于苹果已将其放入其文档中,我认为只要他们更改它,您就非常安全。
这可以在 NSData 类参考中找到: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
I know that this is an old question and that this may be new information that has come up since then, but I'd just like to point something out to all of the people who are claiming that using the description method is a really bad idea. In most cases, you'd be exactly right. The description property is generally just used for debugging, but for the NSData class, it's specifically defined as returning a hexadecimal representation of the receivers contents which is exactly what is needed here. Since Apple has put it in their documentation, I think you're pretty safe as far as them changing it.
This can be found in the NSData Class Reference here: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
我建议您以这种方式将令牌转换为字符串:
更新:
正如许多人提到的,最好使用下一种方法将
NSData *
转换为NSString *
:I suggest you to convert token to string in this way:
UPDATED:
As many people mentioned it is better to use next approach to convert
NSData *
toNSString *
: