Send device token from viewDidLoad to webView Objective C
我有一个应用程序,在
现在我想把设备令牌发送到与
代码如下所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | - (void)viewDidLoad { NSUserDefaults *deviceInfo = [NSUserDefaults standardUserDefaults]; NSString *deviceID = [deviceInfo objectForKey:@"deviceToken"]; [super viewDidLoad]; NSURL *url=[NSURL URLWithString:@"http://staging.mywebsite.com"]; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url]; NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies]; NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary]; [cookieProperties setObject:@"deviceToken" forKey:NSHTTPCookieName]; [cookieProperties setObject:deviceID forKey:NSHTTPCookieValue]; [cookieProperties setObject:@"staging.mywebsite.com" forKey:NSHTTPCookieDomain]; [cookieProperties setObject:@"staging.mywebsite.com" forKey:NSHTTPCookieOriginURL]; [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires]; NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; [request setHTTPMethod:@"Post"]; [request setHTTPShouldHandleCookies:YES]; [request setAllHTTPHeaderFields:headers]; [_webView loadRequest:request]; } |
我在方法
1 2 3 4 5 6 7 8 9 10 11 | - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSString *strDevicetoken = [[NSString alloc]initWithFormat:@"%@",[[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@"" withString:@""]]; NSUserDefaults *deviceInfo = [NSUserDefaults standardUserDefaults]; [deviceInfo setObject:strDevicetoken forKey:@"deviceToken"]; [deviceInfo synchronize]; } |
它仍然不能在真正的设备上工作。我在这里做错什么了?
谢谢你
您当前的情况是,当您在应用程序委托中注册远程通知时,在调用DidRegisterForRemoteMotificationWithDeviceToken之前,您已经初始化了视图控制器(VC),并且在获取设备令牌之前正在调用ViewDidLoad方法。
你的应用程序流程应该是这样的
i.应用程序内委托didFinishLaunchingWithOptions方法,注册远程通知。
二。在带有devicetoken回调函数的didregisterforremotenotifications中,您应该在一秒钟内收到设备令牌。
iii.如果启动视图控制器的应用程序内委托,则可以在视图控制器中执行公共功能:
1 2 3 | - (void)requestWebViewWithToken:(NSString *)token { // You code - storing token in cookies and request webview. } |
iv.只有在获得令牌时,才使用devicetoken调用didregisterforremotenotifications中的函数。有很多选择。您可以使用nsnotificationCenter、自定义委托、公共方法(如上所述)或静态方法。
"But i think reloading UIwebView doesnt seem like good programming. Website inside webview will be loaded first and after receiving deviceToken reloaded,dont like the idea of that"
当需要而不是多余的时候重新加载WebView是可以接受的。对于您的案例,您可以在viewdidload中显示和动画活动指示器视图(加载指示器)。
仅在收到令牌时加载WebView。获取令牌只需1秒钟,因此在获取令牌后等待加载WebView不会导致用户体验缺陷。
最后,您可能需要处理用户没有打开以接收推送通知或没有Internet连接等情况…
解决方案:
在AppDelegate.m中,在DidRegisterforRemoteMotificationWithDeviceToken委托中,
1 2 3 | [[NSNotificationCenter defaultCenter] postNotificationName:@"device_token_notification" object:nil userInfo:@{@"token":@"8057b9be2a0caa8802034369fc6035aac9c577180xxxx"}]; |
在包含WebView的VC中,在ViewDidLoad方法中,添加此代码:
1 2 3 4 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestWebView:) name:@"device_token_notification" object:nil]; |
并创建方法
1 2 3 4 5 6 7 | - (void)requestWebView:(NSNotification *)noti { NSDictionary *dict = [noti userInfo]; NSString *deviceToken = dict[@"token"]; // Store token into cookies. // Request your web. } |
当你不需要接收器时,记得把它取下来。
1 2 3 | - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:@"device_token_notification"]; } |