Skip to content

Commit

Permalink
fix: Proposed fix for adding useWebKit support to getFromResponse/set…
Browse files Browse the repository at this point in the history
…FromResponse (react-native-cookies#72)
  • Loading branch information
Loxrie committed Aug 17, 2020
1 parent 2cdd941 commit f4324ec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ declare module '@react-native-community/cookies' {

export interface CookieManagerStatic {
set(url: string, cookie: Cookie, useWebKit?: boolean): Promise<boolean>;
setFromResponse(url: string, cookie: string): Promise<boolean>;
setFromResponse(url: string, cookie: string, useWebKit?: boolean): Promise<boolean>;

get(url: string, useWebKit?: boolean): Promise<Cookies>;
getFromResponse(url: string): Promise<Cookies>;
getFromResponse(url: string, useWebKit?: boolean): Promise<Cookies>;

clearAll(useWebKit?: boolean): Promise<boolean>;

Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
57 changes: 46 additions & 11 deletions ios/RNCookieManagerIOS/RNCookieManagerIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}];
}

Expand Down

0 comments on commit f4324ec

Please sign in to comment.