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];

ここで、CustomAppDelegateはアプリ デリゲート クラスのstr名前であり、デバイス トークンが格納される合成プロパティ (またはメソッドの名前) です。

電話する前にsharedAppDelegate忘れずにimport "CustomAppDelegate.h"

于 2011-10-18T08:13:30.297 に答える