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

相关问题