如何将设备令牌从AppDelegate传递到UIViewController

时间:2014-05-10 11:45:36

标签: ios uiviewcontroller devicetoken

嗨,在我的应用程序中,我正在获取设备令牌,我正在传递给我的服务器以发送通知我现在想要发送个人通知,我需要从我的UIViewController获取设备令牌请告诉我是否有可能从AppdelegateUIViewController

获取设备令牌

我在Appdelegate

中获取设备令牌的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
     return YES;
}

设备令牌。

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
   {
      const char* data = [deviceToken bytes];
      NSMutableString * token = [NSMutableString string];

    for (int i = 0; i < [deviceToken length]; i++) {
       [token appendFormat:@"%02.2hhX", data[i]];
    }

   NSString *urlString = [NSString stringWithFormat:@"url?token=%@",token];

   NSURL *url = [[NSURL alloc] initWithString:urlString];
   NSLog(@"token %@",urlString);


   NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
   NSLog(@"request %@ ",urlRequest);
   NSData *urlData;
   NSURLResponse *response;
   urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
   NSLog(@"data %@",urlData);

  }

我已经使用了获取设备令牌,请告诉我如何将设备令牌传递给我的UIViewController或如何从我的UIViewController获取设备令牌。

5 个答案:

答案 0 :(得分:5)

使用NSUserDefaults存储对象(值),您可以在任何地方访问它。

AppDelegate(setValue):

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
     [[NSUserDefaults standardUserDefaults] setObject: token forKey:@"deviceID"];
     [[NSUserDefaults standardUserDefaults]synchronize];
}

UIViewController(getValue):

[[NSUserDefaults standardUserDefaults] objectForKey:@"deviceID"];

答案 1 :(得分:4)

在AppDelegate.m类中:

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

    NSString *device = [deviceToken description];
    device = [device stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    device = [device stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"My device is: %@", device);

    [[NSUserDefaults standardUserDefaults] setObject:device forKey:@"MyAppDeviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

在ViewController类中,viewDidLoad方法内部:

[super viewDidLoad];

NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyAppDeviceToken"];
NSLog(@"device token in controller: %@ ", deviceToken);

这在我的设备中完美运行。快乐编码!! :)

答案 2 :(得分:0)

在您的视图中尝试使用此编码加载

NSString* deviceId = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
deviceId = [deviceId stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@",deviceId);

答案 3 :(得分:0)

在委托文件中声明并定义以下方法

#pragma mark - Get / Set Device Token
+ (void)setDeviceToken:(NSString *)token {
    if(token) {
        [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

+ (NSString *)getDeviceToken {
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];
    if(token) {
        return token;
    }
    return @"";
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken方法中 获得令牌后,请致电[AppDelegate setDeviceToken:token];

现在在项目中,在任何视图控制器中,您可以调用NSString *token = [AppDelegate getDeviceToken];来获取已保存的令牌,请注意,我们将AppDelegate称为您的委托文件的名称,我们将其称为类名,因为我们创建了一个设置和获取标记的类方法。

到达时,您可以检查已保存令牌的可用性

NSString *token = [AppDelegate getDeviceToken];
if(token.length) {
    // do something
}

答案 4 :(得分:0)

您可以在任何视图控制器中获取appDelegate实例,并从该实例获取值,如 -

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];