如何在doRegisterForRemoteNotificationsWithDeviceToken以外的方法中使用设备标记?

我通过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”。

我如何将这个设备令牌存储在一个全局variables中,并在其他类或其他方法中使用它。

在您的应用程序委托类定义方法+ (CustomAppDelegate *)sharedAppDelegate的实现应该看起来像这样:

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

其中CustomAppDelegate是您的应用程序委托类的名称。

在方法中,您需要获取strvariables的值,您应该input以下内容:

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

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

在调用sharedAppDelegate之前,不要忘记import "CustomAppDelegate.h"

您可以将设备标记添加到NSUserDefaults字典中,如下所示:

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

这可以通过其他方法访问,例如:

 NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"];