Skip to content
This repository was archived by the owner on Jan 24, 2020. It is now read-only.

Commit 60705ba

Browse files
ubermenschjojoeferraro
authored andcommitted
RFC 6265, section 4.1: Set-Cookie; (#58)
1 parent 3c7b196 commit 60705ba

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

RNCookieManagerIOS/RNCookieManagerIOS.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ @implementation RNCookieManagerIOS
4242
callback(@[[NSNull null]]);
4343
}
4444

45+
RCT_EXPORT_METHOD(getFromResponse:(NSURL *)url callback:(RCTResponseSenderBlock)callback) {
46+
NSURLRequest *request = [NSURLRequest requestWithURL:url];
47+
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init]
48+
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
49+
50+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
51+
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:httpResponse.allHeaderFields forURL:response.URL];
52+
NSMutableDictionary *dics = [NSMutableDictionary dictionary];
53+
54+
for (int i = 0; i < cookies.count; i++) {
55+
NSHTTPCookie *cookie = [cookies objectAtIndex:i];
56+
[dics setObject:cookie.value forKey:cookie.name];
57+
NSLog(@"cookie: name=%@, value=%@", cookie.name, cookie.value);
58+
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
59+
}
60+
callback(@[[NSNull null], dics]);
61+
}];
62+
}
4563

4664
RCT_EXPORT_METHOD(get:(NSURL *)url callback:(RCTResponseSenderBlock)callback) {
4765
NSMutableDictionary *cookies = [NSMutableDictionary dictionary];

android/src/main/java/com/psykar/cookiemanager/CookieManagerModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public void setFromResponse(String url, String value, final Callback callback) t
4646
callback.invoke(null, null);
4747
}
4848

49+
@ReactMethod
50+
public void getFromResponse(String url, Callback callback) throws URISyntaxException, IOException {
51+
get(url, callback);
52+
}
53+
4954
@ReactMethod
5055
public void getAll(Callback callback) throws Exception {
5156
throw new Exception("Cannot get all cookies on android, try getCookieHeader(url)");

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ if (Platform.OS === 'ios') {
1919
var functions = [
2020
'set',
2121
'setFromResponse',
22+
'getFromResponse',
2223
'get',
2324
'getAll',
2425
'clearAll',

0 commit comments

Comments
 (0)