我面临一些奇怪的问题。实际上是在AppDelegate的didRegisterForRemoteNotificationsWithDeviceToken
方法
AppDelegate中的代码如下
- (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");
}
以下是控制台输出
2
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 :(得分:1)
没什么,你注册你的应用程序&#34;推送通知&#34;,并等待来自&#34; Apple&#34;的令牌。收到后,您的应用首先会创建 viewcontroller 。
在方法中:
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
告知您 viewcontroller 有关令牌的信息。您有多个选项,找到哪个更适合您的应用架构。
如果您需要更多详细信息,请参阅有关APNS的精彩教程: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
答案 1 :(得分:1)
答案 2 :(得分:0)
在完成didRegisterForRemoteNotificationsWithDeviceToken
时调用的AppDelegate中设置委托方法。
将您的控制器附加到代表,因为它会在通知注册时得到通知。
答案 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;
}
}