2

我通過didRegisterForRemoteNotificationsWithDeviceToken方法獲得了設備令牌。我想用另一種方法使用設備令牌。我嘗試了這種方式,如何在doRegisterForRemoteNotificationsWithDeviceToken以外的方法中使用設備標記?

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」。

如何將此設備令牌存儲在全局變量中並將其用於其他類或其他方法中。

回答

2

在應用程序委託類中定義的方法+ (CustomAppDelegate *)sharedAppDelegate其實施應該是這樣的一個:

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

其中CustomAppDelegate是您的應用程序委託類的名稱。

在方法你需要得到str變量的值,你應該鍵入以下內容:

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

其中CustomAppDelegate是您的應用程序委託類和str的名字合成(方法或名稱)屬性,其中設備令牌被儲存了。

在致電sharedAppDelegate不要忘記import "CustomAppDelegate.h"

+0

這是打開應用程序啓動屏幕,但所有空白的視圖控制器不加載,是否有任何替代的例子,你可以給我,因爲我有e嘗試了這一點,但 - >應用程序啓動 - > DidfinishlaunchwithOption調用 - >初始視圖控制器不呼籲viewdidload如果我把這[[UIapplication sharedApplication] setDelegate:self]完成啓動 –

2

您可以將設備令牌添加到NSUserDefaults字典,像這樣:

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

這然後可以在其他方法,像這樣來訪問:

NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"]; 
相關問題