重定向响应中设置的Cookie不会保留

时间:2019-01-12 19:17:57

标签: session-cookies react-native-ios

这似乎仅在iOS上是一个相关问题:由于将React Native从0.55更新到0.57。 添加凭据:“ include”有帮助,但是如果您重新打开应用程序,则此标志无济于事。重新启动应用程序后,Cookie将被删除。

下面的链接实际上提出了有关此问题的PR版本,但是即使那样也不能解决错误。 https://github.com/facebook/react-native/commit/a6860487947ae0957f5dfa4979e92bc7740fecb0

这是文件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);
}

0 个答案:

没有答案