1
votes

Bug description: My WebView should display a web page that has a cookie authentication. The page expects a session-id cookie acquired on login. To authenticate I send the login request to the website using fetch().

When the login is successful I can see that proper cookies are received. On success, I'm starting the WebView.

It works perfectly on Android but not on iOS.

I tested it on iOS 9 and iOS 11 simulators, and on an iOS 11 device.

To sum up the question: Why cookies I've got with fetch aren't passed to WebView on iOS only.

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

enter image description here

Environment:

  • OS: IOS
  • OS version: 14.4.2
  • react-native version: 0.64
  • react-native-webview version: ^11.4.3
1

1 Answers

0
votes

I used getAllCookies to get cookies from wkwebview, like 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];
      }];
}