Sha*_*anu 4 uiviewcontroller devicetoken ios
嗨,在我的应用程序中我正在获取设备令牌,我正在传递给我的服务器现在发送通知我想发送个人通知我需要获取设备令牌表格我UIViewController请告诉是否有任何可能获取设备令牌形成Appdelegate或来自UIViewController
我在中获取设备令牌的代码 Appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
设备令牌.
-(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);
}
Run Code Online (Sandbox Code Playgroud)
我已经使用了获取设备令牌,请告诉我如何将设备令牌传递给我UIViewController或如何从我的设备令牌中获取设备令牌UIViewController.
使用NSUserDefaults存储对象(值),您可以在任何地方访问它.
AppDelegate(setValue):
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
[[NSUserDefaults standardUserDefaults] setObject: token forKey:@"deviceID"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
Run Code Online (Sandbox Code Playgroud)
UIViewController(getValue):
[[NSUserDefaults standardUserDefaults] objectForKey:@"deviceID"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4827 次 |
| 最近记录: |