Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Proposed fix for adding useWebKit support to getFromResponse/set… #74

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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