Cookies set in a redirect response are not persist

2019-07-28 11:47发布

问题:

This appears to be a related issue only on iOS: since updating React Native from 0.55 to 0.57. adding credentials: 'include' helps, but this flag does not help if you reopen the app. Cookie will be deleted after app relaunch.

Below link actually proposes a PR release around this, but even that is not resolving the error. https://github.com/facebook/react-native/commit/a6860487947ae0957f5dfa4979e92bc7740fecb0

This is addition to the file react-native/Libraries/Network/RCTHTTPRequestHandler.mm

- (void)URLSession:(NSURLSession *)session
              task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
        newRequest:(NSURLRequest *)request
 completionHandler:(void (^)(NSURLRequest *))completionHandler
{
  // Add the cookies to the new request
  // This is necessary because we're not letting iOS handle cookies by itself
  NSMutableURLRequest *nextRequest = [request mutableCopy];

  NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL];
  nextRequest.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
  completionHandler(nextRequest);
}