Ios 如何在DidRegisterForRemotionTificationsWithDeviceToken以外的方法中使用设备令牌?
我通过Ios 如何在DidRegisterForRemotionTificationsWithDeviceToken以外的方法中使用设备令牌?,ios,iphone,objective-c,apple-push-notifications,appdelegate,Ios,Iphone,Objective C,Apple Push Notifications,Appdelegate,我通过didRegisterForRemoteNotificationsWithDeviceToken方法获得了设备令牌。我想在另一种方法中使用设备令牌。我就这样试过, 在didRegisterForRemotonificationswithDeviceToken方法中: str = [NSString stringWithFormat:@"%@",deviceToken]; // str is the NSString which is declared in the appDelegate.
didRegisterForRemoteNotificationsWithDeviceToken
方法获得了设备令牌。我想在另一种方法中使用设备令牌。我就这样试过,
在didRegisterForRemotonificationswithDeviceToken
方法中:
str = [NSString stringWithFormat:@"%@",deviceToken];
// str is the NSString which is declared in the appDelegate.h file as global variable
NSLog(@"Device Token : %@",str);
在didReceiveMemotentification
方法中:
str = [NSString stringWithFormat:@"%@",deviceToken];
// str is the NSString which is declared in the appDelegate.h file as global variable
NSLog(@"Device Token : %@",str);
当我这样做时,设备令牌
返回为“nosniff”
如何将此设备令牌存储在全局变量中,并在其他类或其他方法中使用它。在您的应用程序委托类中,定义方法
+(CustomAppDelegate*)sharedAppDelegate
,其实现应如下所示:
+ (CustomAppDelegate *)sharedAppDelegate
{
return (CustomAppDelegate *) [UIApplication sharedApplication].delegate;
}
其中CustomAppDelegate
是应用程序代理类的名称
在需要获取str
变量值的方法中,应键入以下内容:
NSString *token = [[CustomAppDelegate sharedAppDelegate] str];
其中,CustomAppDelegate
是应用程序委托类的名称,str
是存储设备令牌的合成属性(或方法名称)
调用
sharedAppDelegate
之前,不要忘记导入“CustomAppDelegate.h”您可以将设备令牌添加到NSUserDefaults
字典中,如下所示:
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
[[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"deviceToken"];
然后可以通过以下其他方法访问:
NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"];
这将打开应用程序启动屏幕,但所有空白视图控制器均未加载,如果我将此[[UIapplication sharedApplication]setDelegate:self]放入,您是否可以给出其他示例,因为我已尝试过此操作,但-->应用程序启动-->DidFinishLaunchWith选项名为-->初始视图控制器未调用viewdidload年,他完成了发射