2014-10-10 97 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; 

} 
} 
相關問題