How to transfer device token from AppDelegate to UIViewController

Hello in my application I am retrieving a device token and I am going to my server to send a notification now. I want to send an individual notification to receive the device token form. UIViewControllerPlease advise if it is possible to extract the device token from Appdelegateor fromUIViewController

My code to load the device token in Appdelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
     return YES;
}

Device id

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

  }

I used to get the device token, please tell me how to transfer the device token to mine UIViewControlleror how to extract the device token from mine UIViewController.

+4
source share
5 answers

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"];
+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);

. !!:)

+4

,

NSString* deviceId = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
deviceId = [deviceId stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@",deviceId);
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 call, [AppDelegate setDeviceToken:token]; ,

Now in the project, in any view controller, you can call NSString *token = [AppDelegate getDeviceToken];to get the saved token, here, note that we call it with AppDelegateyour delegate file name, and we call it with the class name, because we create a class method to set and receive the token .

At the time of receipt, you can check for the presence of a stored token

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

You can get an appDelegate instance in any contoller and fetch view from that instance, for example -

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

Source: https://habr.com/ru/post/1540024/


All Articles