【问题标题】:WKWebview cookie issue?WKWebview cookie 问题?
【发布时间】:2020-11-09 06:28:09
【问题描述】:

背景:

我正在升级我的应用程序以使用 UIWebview 中的 WKWebview,因为它将no longer be accepted by App Store

自 2020 年 4 月起,App Store 将不再接受使用 UIWebView 的新应用,自 2020 年 12 月起将不再接受使用 UIWebView 的应用更新。

问题:

发生的情况是,我在登录后从 Webview (WKWebview) 获取 cookie 返回。我将检索 API 触发所需的令牌,但是我经常遇到 HTTP 401 状态触发的所有 API。

如果我恢复到 UIWebview,并重复相同的登录但重新使用 WKWebview 中的 SAME TOKEN。我正在获得 HTTP 200 状态。重复使用相同令牌的目的是证明这是一个有效的令牌。

请注意,我没有对方法调用 API 进行任何更改。 WKWebview 和 UIWebview 保持不变。 (包括POST方式的对象请求)

我在 WKWebview 中遗漏了什么吗?

我是否需要设置 cookie 或允许任何特定的东西?

代码 sn-p:

<WebView
            style={{ width: this.state.webviewWidth }}
            ref={(component) => {
              this.webviewRef = component;
            }}
            source={{ uri: this.state.url }}
            onLoadStart={this.webviewOnLoadStart}
            onLoad={this.webviewOnLoadEnd}
            onNavigationStateChange={this.onNavigationStateChange}
            useWebKit={true}
            sharedCookiesEnabled={true}
          />

打包.json

"react-native": "0.61.5",
"react-native-webview": "^10.3.2",
"@react-native-community/cookies": "^3.0.0",

【问题讨论】:

    标签: react-native webkit wkwebview react-native-ios


    【解决方案1】:

    显然,问题在于 WKWebview 没有为我们处理传入请求标头的 cookie。因此,每次到达服务器时,我都会收到 401 身份验证失败。

    不幸的是,React-Native-Webview 没有办法处理这个 cookie 标头的开箱即用(如果我错了,请纠正我)。我完全了解库中提供的这个“custom header”函数,不幸的是它没有按预期工作。 cookie 仍然不是请求标头的一部分。

    我对此的解决方案如下,在方法@​​987654323@下进行:

    1. 深入了解原生库 (React-Native-Webview)
    2. 循环遍历websiteDataStore.httpCookieStore 中的所有现有cookie
    3. 寻找我的cookie(当然,你应该知道你的cookie名称)
    4. 将 cookie 附加到您的 http 请求的 HTTPHeaderField
    5. 再次加载http请求!
    if (@available(iOS 11.0, *)) {
              [webView.configuration.websiteDataStore.httpCookieStore getAllCookies:^(NSArray<NSHTTPCookie *> * _Nonnull cookies) {
                  NSLog(@"All cookies in websiteDataStore %@", cookies);
                  for(NSHTTPCookie *cookie in cookies){
                      if([cookie.name isEqualToString:@"coookieName"]){
                          if([navigationAction.request valueForHTTPHeaderField:@"Cookie"] != nil ){
                              
                          }else{
                              NSMutableURLRequest *req = [request mutableCopy];
                              NSString *newCookie = [NSString stringWithFormat:@"cookieName=%@" , cookie.value];
    
                              // As you can see, I'm appending the cookie value into header myself 
                              [req addValue:newCookie forHTTPHeaderField:@"Cookie"];
                              [req setHTTPShouldHandleCookies:YES];
                              [webView loadRequest:req];
                              return;
                          }
                      }
                  }
              }];
          } else {
              // Fallback on earlier versions
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 2015-03-17
      相关资源
      最近更新 更多