2

didRegisterForRemoteNotificationsWithDeviceTokenメソッドでデバイストークンを取得しました。私は別の方法でデバイストークンを使いたかったのです。私はこのようなDevice Tokenは "nosniff" と返しているんdidRegisterForRemoteNotificationsWithDeviceToken以外の方法でデバイストークンを使用する方法?

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

didReceiveRemoteNotification方法で

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

:私は、このようにdidRegisterForRemoteNotificationsWithDeviceToken方法で

を、それを試してみました。

このデバイストークンをグローバル変数に格納し、それを他のクラスまたは他の方法で使用するにはどうすればよいですか。

CustomAppDelegateは、アプリのデリゲートクラスの名前です
+ (CustomAppDelegate *)sharedAppDelegate 
{ 
    return (CustomAppDelegate *) [UIApplication sharedApplication].delegate; 
} 

:アプリのデリゲートクラスで

答えて

2

は、その実装のこの1のようになります方法+ (CustomAppDelegate *)sharedAppDelegateを定義します。この方法で

あなたは次のように入力する必要がありstr変数の値を得るためにあなたの必要性:

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

CustomAppDelegateは、アプリのデリゲートクラスの名前で、strが合成されたプロパティ(またはメソッドの名前)ですデバイストークン保存されています。 sharedAppDelegateを呼び出す前に

あなたがそうのようなNSUserDefaults辞書にデバイストークンを追加することができますimport "CustomAppDelegate.h"

+0

これはアプリの起動画面を開いていますが、すべてのビューコントローラが読み込まれていない場合は空白にすることができます私はこの[[UIapplication sharedApplication] setDelegate:self]を実行したら、これを試しましたが、 - > App launch - > DidfinishlaunchwithOnption Called - >初期View Controllerがviewdidloadのために呼び出されませんでした –

2

することを忘れないでください:

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

これは、そのようにのような他の方法でアクセスすることができます。

NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"]; 
関連する問題