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
.
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"];
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
}
您可以在任何视图控制器中获取appDelegate实例,并从该实例获取值,如 -
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.