1

我不知道如何讓設備令牌(APN)內獲得設備 - (BOOL)應用中:didFinishLaunchingWithOptions:

- (BOOL)application:didFinishLaunchingWithOptions: 

唯一的地方,我可以得到的AppDelegate設備令牌是

- (void)application:didRegisterForRemoteNotificationsWithDeviceToken: 

其中應用程序:didRegisterForRemoteNotificationsWithDeviceToken將運行異步或在didFinishLaunchingWithOptions之後。那麼,有沒有辦法在didFinishLaunchingWithOptions中獲取設備標記?因爲我想將它傳遞到從didFinishLaunchingWithOptions中推送的ViewController。

這裏是我的示例代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    for (id key in launchOptions) { 
     NSLog(@"Key: %@, Value %@", key, [launchOptions objectForKey: key]); 
    } 
    AddViewController *avc = [[AddViewController alloc] init]; 
    avc.managedObjectContext = self.managedObjectContext; 
    avc.pushToken = self.pushToken; 
    UINavigationController *uinav = [[UINavigationController alloc] init ]; 

    [uinav pushViewController:avc animated:YES]; 
    [_window addSubview:uinav.view]; 
    [avc release]; 

    [self.window makeKeyAndVisible]; 

    // Let the device know we want to receive push notifications 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
    return YES; 
} 

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
    NSLog(@"My token is: %@", deviceToken); 
    self.pushToken = [NSString stringWithFormat:@"%@", deviceToken]; 
} 

非常感謝任何幫助。

回答

2

application:didRegisterForRemoteNotificationsWithDeviceToken:是您獲得當前設備令牌的唯一機會。但是,您可以將結果存儲在NSUserDefaults中,並在啓動時從該處讀取結果。大多數情況下,如果應用程序尚未卸載/重新安裝,設備令牌在啓動時保持不變。

+0

好主意warrenm。 TX – dejoong 2012-04-27 03:18:13

相關問題