2

我通过didRegisterForRemoteNotificationsWithDeviceToken方法获得了设备令牌。我想以另一种方法使用设备令牌。我是这样试的

didRegisterForRemoteNotificationsWithDeviceToken方法:

str = [NSString stringWithFormat:@"%@",deviceToken];
// str is the NSString which is declared in the appDelegate.h file as global variable

didReceiveRemoteNotification方法:

 NSLog(@"Device Token : %@",str);  

当我这样做时,它Device Token会以“nosniff”的形式返回。

如何将此设备令牌存储在全局变量中并在其他类或其他方法中使用它。

4

2 回答 2

3

您可以像这样将设备令牌添加到NSUserDefaults字典中:

-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"deviceToken"];

然后可以通过其他方法访问它,例如:

NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"];
于 2013-02-10T04:30:36.847 回答
2

在您的应用程序委托类中定义+ (CustomAppDelegate *)sharedAppDelegate其实现应如下所示的方法:

+ (CustomAppDelegate *)sharedAppDelegate
{
     return (CustomAppDelegate *) [UIApplication sharedApplication].delegate;
}

CustomAppDelegate您的应用委托类的名称在哪里。

在方法中,您需要获取str变量的值,您应该键入以下内容:

NSString *token = [[CustomAppDelegate sharedAppDelegate] str];

whereCustomAppDelegate是您的应用程序委托类的str名称,并且是存储设备令牌的综合属性(或方法的名称)。

打电话前sharedAppDelegate别忘了import "CustomAppDelegate.h"

于 2011-10-18T08:13:30.297 回答