繁体   English   中英

ios DeviceToken:如何将DeviceToken从AppDelegate传递到ViewController?

[英]ios DeviceToken: How to pass deviceToken from AppDelegate to ViewController?

我面临一些奇怪的问题。 实际上是在AppDelegate的didRegisterForRemoteNotificationsWithDeviceToken方法之前调用的didRegisterForRemoteNotificationsWithDeviceTokendidRegisterForRemoteNotificationsWithDeviceToken方法

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

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

没什么,您可以为应用程序注册“推送通知”,然后等待来自“ Apple”的令牌。 收到正常消息后,您的应用将首先创建您的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. 使用可以将令牌保存在NSUserdefault,文件,数据库(SQLite,Coredata,文件等...)中,简单,您可以将其保存在全局变量中,单例...
  2. 您可以使用“推送通知”来通知应用程序代理成功注册远程通知的时间。
  3. 您可以定义协议以监听应用程序委托成功注册以进行远程通知。

完成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];

[[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;

 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM