4

嗨,在我的应用程序中,我正在获取设备令牌,我正在传递给我的服务器以发送通知现在我想发送个人通知,因为我需要从我的表格中获取设备令牌UIViewController请告诉是否有任何可能获取设备令牌形成Appdelegate或来自UIViewController

我用于获取设备令牌的代码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.

4

5 回答 5

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"];
于 2014-05-10T11:49:32.767 回答
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);

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

于 2014-06-27T06:28:57.333 回答
0

在您的视图中尝试此编码确实加载

NSString* deviceId = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
deviceId = [deviceId stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@",deviceId);
于 2014-05-10T11:53:06.420 回答
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
}
于 2014-05-10T12:04:52.073 回答
0

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

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
于 2014-05-10T12:18:13.143 回答