2014-10-10 103 views
1

我正面临一些奇怪的问题。前AppDelegate中的didRegisterForRemoteNotificationsWithDeviceToken方法其实叫ios DeviceToken:如何将deviceToken从AppDelegate传递给ViewController?

代码在AppDelegate中的viewController的viewDidLoad方法如下

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
    NSLog(@"My token is: %@", deviceToken); 

    NSString* newToken = [deviceToken description]; 
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSLog(@"token:%@",newToken); 
    NSUserDefaults *defaultValues = [NSUserDefaults standardUserDefaults]; 
    [defaultValues setValue:newToken forKey:key_device_token]; 
    [defaultValues synchronize]; 
} 

viewDidLoad方法

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    NSLog(@"ABCD"); 
} 

的代码下面是控制台输出

014-10-10 16:59:15.590 FollowMe[650:60b] ABCD 
2014-10-10 16:59:15.592 FollowMe[650:60b] app dir: file:///var/mobile/Applications/94B3DF5E-B0CB-4F0B-99E7-2DFEBDC30ECB/Documents/ 
2014-10-10 16:59:15.693 FollowMe[650:60b] My token is: <3fff5f77 d15d7680 f8028b92 d1ebaf9b 06457115 336f1ee5 56172de6 5d8217c5> 
2014-10-10 16:59:15.695 FollowMe[650:60b] token:3fff5f77d15d7680f8028b92d1ebaf9b06457115336f1ee556172de65d8217c5 

任何人都可以告诉我什么是我的代码的问题?

回答

0

在完成didRegisterForRemoteNotificationsWithDeviceToken时调用您在AppDelegate中调用的委托方法。 将您的控制器连接到委托,因为它会在通知注册时得到通知。

0

没什么,您注册您的应用程序的“推送通知”,并等待来自“Apple”的令牌。当你收到它是正常的你的应用程序首先创建您的视图控制器

在该方法中:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 

通知您视图 - 控制约令牌。您有多个选项,找到哪个更适合您的应用程序体系结构。

如果您需要更多的细节,关于APNS一个伟大的教程: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

1
  1. 使用可以节省NSUserdefault令牌,文件,数据库(SQLite的,Coredata,文件等),简单,你可以将其保存在全局变量中,单身...
  2. 您可以使用推送通知来通知应用程序委托成功注册远程通知。
  3. 您可以定义协议来侦听应用程序委托成功注册以进行远程通知。
0
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
NSLog(@"My token is: %@", deviceToken); 

NSString* newToken = [deviceToken description]; 
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
NSLog(@"token:%@",newToken); 
NSUserDefaults *defaultValues = [NSUserDefaults standardUserDefaults]; 
[defaultValues setValue:newToken forKey:key_device_token]; 
[defaultValues synchronize]; 

[[NSNotificationCenter defaultCenter] postNotificationName:@"profileUpdated" object:nil]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(printDeviceID:) 
               name:@"profileUpdated" 
               object:nil]; 
} 


- (void) printDeviceID:(NSNotification *) notification 
{ 
    if ([notification.name isEqualToString:@"profileUpdated"]) 
{ 
    NSUserDefaults *defaultValues = notification.info; 

} 
} 
相关问题