【问题标题】:On iOS missing `session cookies` used React Native WebView在 iOS 上缺少“会话 cookie”,使用了 React Native WebView
【发布时间】:2021-09-09 21:20:16
【问题描述】:

错误说明: 我的 WebView 应该显示一个具有 cookie 身份验证的网页。该页面需要在登录时获取会话 ID cookie。为了进行身份验证,我使用 fetch() 将登录请求发送到网站。

登录成功后,我可以看到收到了正确的 cookie。成功后,我将启动 WebView。

它在 Android 上完美运行,但在 iOS 上却不行。

我在 iOS 9 和 iOS 11 模拟器以及 iOS 11 设备上对其进行了测试。

总结一下这个问题:为什么我通过 fetch 获得的 cookie 不能仅传递给 iOS 上的 WebView。

<WebView 
    source = {{
        uri: url,
    }}
    onLoadEnd={() => {
        CookieManager.getAll().then((cookies) => {
            console.log('CookieManager.getAll =>', cookies);
        });
    }}
/>

enter image description here

环境:

  • 操作系统:IOS
  • 操作系统版本:14.4.2
  • react-native 版本:0.64
  • react-native-webview 版本:^11.4.3

【问题讨论】:

    标签: ios react-native cookies webview


    【解决方案1】:

    我使用getAllCookies 从 wkwebview 获取 cookie,例如 this

    if (@available(iOS 11.0, *)) {
      [webView.configuration.websiteDataStore.httpCookieStore
          getAllCookies:^(NSArray<NSHTTPCookie *> *_Nonnull cookies) {
            NSURLRequest *request =
                [[NSURLRequest alloc] initWithURL:self.URL]; //your URL
            NSURLSession *session = [NSURLSession sharedSession];
            NSURLSessionDataTask *task = [session
                dataTaskWithRequest:request
                  completionHandler:^(NSData *responseData, NSURLResponse *response,
                                      NSError *error) {
                    //Do Something
                  }];
            [task resume];
            [session.configuration.HTTPCookieStorage storeCookies:cookies forTask:task];
          }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2021-09-05
      • 2018-02-05
      • 2022-10-20
      • 2018-05-25
      • 2012-04-07
      • 1970-01-01
      相关资源
      最近更新 更多