gpt4 book ai didi

ios - 将设备 token 从 viewDidLoad 发送到 webView Objective C

转载 作者:行者123 更新时间:2023-11-29 00:32:17 28 4
gpt4 key购买 nike

我有一个应用程序,其中在 viewController 上有一个简单的 webView,我正在加载我的网站说 https://mywebsite.com

现在我想将设备 token 发送到与 cookie 相同的网站,但由于某种原因我无法在 viewDidLoad 方法中访问 deviceToken。

代码如下viewController.m

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

我在方法 didRegisterForRemoteNotificationsWithDeviceToken 中添加了以下代码

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

}

它仍然不能在真实设备上运行。我在这里做错了什么?

谢谢

最佳答案

您目前的情况是,当您在应用委托(delegate)中注册远程通知时,在调用 didRegisterForRemoteNotificationsWithDeviceToken 之前,您已经启动了 View Controller (VC),并且在获取设备 token 之前调用了 viewDidLoad 方法。

你的应用流程应该是这样的

我。在应用委托(delegate) didFinishLaunchingWithOptions 方法中,注册远程通知。

二。在一秒钟内,在 didRegisterForRemoteNotificationsWithDeviceToken 回调函数中,您应该会收到设备 token 。

三。如果您在应用委托(delegate)中启动您的 View Controller ,您可以在您的 View Controller 中创建一个公共(public)函数:

- (void)requestWebViewWithToken:(NSString *)token {
// You code - storing token in cookies and request webview.
}

四。仅当您获得 token 时调用 didRegisterForRemoteNotificationsWithDeviceToken 中的函数。有很多选择。您可以使用 NSNotificationCenter , custom delegate 、公共(public)方法(如上所述)或 static method .

"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 中显示和动画事件指示器 View (加载指示器)。

仅当您收到 token 时才加载 webview。获取token只需要1秒,所以获取token后等待加载webview不会造成用户体验缺陷。

最后,您可能需要处理用户没有打开接收推送通知,或没有互联网连接等情况......


解决方法:

  1. 在 AppDelegate.m 中,在您的didRegisterForRemoteNotificationsWithDeviceToken 委托(delegate)中,

    [[NSNotificationCenter defaultCenter] postNotificationName:@"device_token_notification"
    object:nil
    userInfo:@{@"token":@"8057b9be2a0caa8802034369fc6035aac9c577180xxxx"}];
  2. 在包含 webview 的 vc 中,在 viewDidLoad 方法中,添加此代码:

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(requestWebView:)
    name:@"device_token_notification"
    object:nil];

并创建方法

- (void)requestWebView:(NSNotification *)noti {
NSDictionary *dict = [noti userInfo];
NSString *deviceToken = dict[@"token"];

// Store token into cookies.
// Request your web.
}

记得在不需要时取下接收器。

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:@"device_token_notification"];
}

关于ios - 将设备 token 从 viewDidLoad 发送到 webView Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454246/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com