【问题标题】:How to pass the device token from AppDelegate to UIViewController如何将设备令牌从 AppDelegate 传递给 UIViewController
【发布时间】:2014-06-28 03:22:23
【问题描述】:

嗨,在我的应用程序中,我正在获取设备令牌,我正在传递给我的服务器以发送通知现在我想发送单独的通知,因为我需要从我的 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 获取设备令牌。

【问题讨论】:

    标签: ios uiviewcontroller devicetoken


    【解决方案1】:

    使用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"];
    

    【讨论】:

    • 请在 appdelegate 中告诉我我在哪里使用了 nsuserdefault 我必须在哪种方法中声明
    • 注册didRegisterForRemoteNotificationsWithDeviceToken:
    • 我使用了上面的代码,我面临两个问题,一个是在 NSuserdefault 上显示错误以替换为 Nsuserdefaults,然后在标准中替换了它的显示警告
    • 在标准中显示为类方法 +standarddefault 未找到(返回类型默认为 toid)
    • 对不起,我刚刚输入了 .所以它的拼写错误。立即查看
    【解决方案2】:

    在你的视图中尝试这个编码确实加载了

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

    【讨论】:

    • OP 不需要新的 deviceId,他已经从远程通知委托中获得了一个。顺便说一句,identifierForVendor 是特定于应用程序供应商的,一旦来自同一供应商的所有应用程序都从设备中删除,它就会发生变化。
    【解决方案3】:

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

    #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
    }
    

    【讨论】:

    • 为什么需要检查 everyTime ` if(token) ` 等。 didRegisterForRemoteNotificationsWithDeviceToken 只有在有 tokenId 时才能调用。
    • 是的,在这种情况下确实如此,但我并没有真正为我的应用程序和我的代码承担任何风险,如果以防万一,我将得到 nil,我的应用程序不会崩溃!这是我在做任何事情之前检查对象有效性的做法。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      在 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);
      

      这在我的设备上运行良好。快乐编码! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-01
        • 2023-03-30
        • 1970-01-01
        • 2010-12-11
        • 1970-01-01
        • 2016-06-22
        • 1970-01-01
        • 2018-10-23
        相关资源
        最近更新 更多