Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass the device token from AppDelegate to UIViewController

Hi in my application I'm fetching the device token and I'm passing to my server to send the notification now I want to send the individual notification for the i need to fetch the device token form my UIViewController Please tell is there any possibilities fetching the device token form the Appdelegate or from the UIViewController

My code for fetching the device token in Appdelegate

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

Device Token.

-(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 have used the to get the device token please tell me how to pass the device token to my UIViewController or how to fetch the device token from my UIViewController.

like image 327
Shanthanu Avatar asked Jan 10 '23 15:01

Shanthanu


2 Answers

Use the NSUserDefaults to store the objects(values), you can access it anywhere.

AppDelegate (setValue) :

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

UIViewController (getValue) :

[[NSUserDefaults standardUserDefaults] objectForKey:@"deviceID"];
like image 188
Kumar KL Avatar answered Jan 19 '23 04:01

Kumar KL


In AppDelegate.m class:

- (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];
}

In ViewController class, inside viewDidLoad method:

[super viewDidLoad];

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

This is working perfectly in my device. Happy Coding !! :)

like image 41
Anu Padhye Avatar answered Jan 19 '23 05:01

Anu Padhye