Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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 !! :)

Try this coding in your view did load

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

Declare and define below methods in your delegate file,

#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 @"";
}

In -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken method call, [AppDelegate setDeviceToken:token]; once you get the token and

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

At the time of getting you can check for availability of saved token

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

You can get appDelegate instance in any view contoller and fetch value from that instance like -

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top