diff --git a/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java b/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java index 778d48c..0db8a20 100644 --- a/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java +++ b/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java @@ -76,7 +76,7 @@ public void set(String url, ReadableMap cookie, Boolean useWebKit, final Promise } @ReactMethod - public void setFromResponse(String url, String cookie, final Promise promise) { + public void setFromResponse(String url, String cookie, Boolean useWebKit, final Promise promise) { if (cookie == null) { promise.reject(new Exception(INVALID_COOKIE_VALUES)); return; @@ -96,7 +96,7 @@ public void flush(Promise promise) { } @ReactMethod - public void getFromResponse(String url, Promise promise) throws URISyntaxException, IOException { + public void getFromResponse(String url, Boolean useWebkit, Promise promise) throws URISyntaxException, IOException { promise.resolve(url); } diff --git a/index.d.ts b/index.d.ts index 205a9aa..7eb8aa5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -16,10 +16,10 @@ declare module '@react-native-community/cookies' { export interface CookieManagerStatic { set(url: string, cookie: Cookie, useWebKit?: boolean): Promise; - setFromResponse(url: string, cookie: string): Promise; + setFromResponse(url: string, cookie: string, useWebKit?: boolean): Promise; get(url: string, useWebKit?: boolean): Promise; - getFromResponse(url: string): Promise; + getFromResponse(url: string, useWebKit?: boolean): Promise; clearAll(useWebKit?: boolean): Promise; diff --git a/index.js b/index.js index b83ae69..31d4b9d 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,10 @@ module.exports = { get: (url, useWebKit = false) => CookieManager.get(url, useWebKit), set: (url, cookie, useWebKit = false) => CookieManager.set(url, cookie, useWebKit), + setFromResponse: (url, cookie, useWebKit = false) => + CookieManager.setFromResponse(url, cookie, useWebKit), + getFromResponse: (url, useWebKit = false) => + CookieManager.getFromResponse(url, useWebKit), clearByName: (url, name, useWebKit = false) => CookieManager.clearByName(url, name, useWebKit), flush: async () => { diff --git a/ios/RNCookieManagerIOS/RNCookieManagerIOS.m b/ios/RNCookieManagerIOS/RNCookieManagerIOS.m index ae3acc6..2b60980 100644 --- a/ios/RNCookieManagerIOS/RNCookieManagerIOS.m +++ b/ios/RNCookieManagerIOS/RNCookieManagerIOS.m @@ -78,32 +78,67 @@ + (BOOL)requiresMainQueueSetup RCT_EXPORT_METHOD( setFromResponse:(NSURL *)url cookie:(NSString *)cookie + useWebKit:(BOOL)useWebKit resolver:(RCTPromiseResolveBlock)resolve - rejecter:(RCTPromiseRejectBlock)reject) { - + rejecter:(RCTPromiseRejectBlock)reject) +{ NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:@{@"Set-Cookie": cookie} forURL:url]; - [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:nil]; - resolve(@(YES)); + if (useWebKit) { + if (@available(iOS 11.0, *)) { + dispatch_async(dispatch_get_main_queue(), ^(){ + WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore]; + for (NSHTTPCookie* cookie in cookies) + { + [cookieStore setCookie:cookie completionHandler:nil]; + } + resolve(@(YES)); + }); + } else { + reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil); + } + } else { + [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:nil]; + resolve(@(YES)); + } } RCT_EXPORT_METHOD( getFromResponse:(NSURL *)url + useWebKit:(BOOL)useWebKit resolver:(RCTPromiseResolveBlock)resolve - rejecter:(RCTPromiseRejectBlock)reject) { + rejecter:(RCTPromiseRejectBlock)reject) +{ NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:httpResponse.allHeaderFields forURL:response.URL]; - NSMutableDictionary *dics = [NSMutableDictionary dictionary]; + if (useWebKit) { + if (@available(iOS 11.0, *)) { + dispatch_async(dispatch_get_main_queue(), ^(){ + NSMutableDictionary *dics = [NSMutableDictionary dictionary]; + WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore]; + for (int i = 0; i < cookies.count; i++) { + NSHTTPCookie *cookie = [cookies objectAtIndex:i]; + [dics setObject:cookie.value forKey:cookie.name]; + [cookieStore setCookie:cookie completionHandler:nil]; + } + resolve(dics); + }); + } else { + reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil); + } + } else { + NSMutableDictionary *dics = [NSMutableDictionary dictionary]; + for (int i = 0; i < cookies.count; i++) { + NSHTTPCookie *cookie = [cookies objectAtIndex:i]; + [dics setObject:cookie.value forKey:cookie.name]; + [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; + } + resolve(dics); - for (int i = 0; i < cookies.count; i++) { - NSHTTPCookie *cookie = [cookies objectAtIndex:i]; - [dics setObject:cookie.value forKey:cookie.name]; - [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; } - resolve(dics); }]; }