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

Fixing subdomain issues #114 #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 4 additions & 29 deletions ios/RNCookieManagerIOS/RNCookieManagerIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,6 @@ + (BOOL)requiresMainQueueSetup
}];
}

-(NSString *)getDomainName:(NSURL *) url
{
NSString *separator = @".";
NSInteger maxLength = 2;

NSURLComponents *components = [[NSURLComponents alloc]initWithURL:url resolvingAgainstBaseURL:FALSE];
NSArray<NSString *> *separatedHost = [components.host componentsSeparatedByString:separator];
NSInteger count = [separatedHost count];
NSInteger endPosition = count;
NSInteger startPosition = count - maxLength;

NSMutableString *result = [[NSMutableString alloc]init];
for (NSUInteger i = startPosition; i != endPosition; i++) {
[result appendString:separator];
[result appendString:[separatedHost objectAtIndex:i]];
}
return result;
}

RCT_EXPORT_METHOD(
get:(NSURL *) url
useWebKit:(BOOL)useWebKit
Expand All @@ -128,13 +109,13 @@ -(NSString *)getDomainName:(NSURL *) url
if (useWebKit) {
if (@available(iOS 11.0, *)) {
dispatch_async(dispatch_get_main_queue(), ^(){
NSString *topLevelDomain = [self getDomainName:url];

WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];

[cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> *allCookies) {
NSMutableDictionary *cookies = [NSMutableDictionary dictionary];

for(NSHTTPCookie *currentCookie in allCookies) {
if([currentCookie.domain containsString:topLevelDomain]) {
if ([url.host containsString:currentCookie.domain]) {
[cookies setObject:currentCookie.value forKey:currentCookie.name];
}
}
Expand All @@ -147,13 +128,7 @@ -(NSString *)getDomainName:(NSURL *) url
} else {
NSMutableDictionary *cookies = [NSMutableDictionary dictionary];
for (NSHTTPCookie *c in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url]) {
NSMutableDictionary *d = [NSMutableDictionary dictionary];
[d setObject:c.value forKey:@"value"];
[d setObject:c.name forKey:@"name"];
[d setObject:c.domain forKey:@"domain"];
[d setObject:c.path forKey:@"path"];
[d setObject:[self.formatter stringFromDate:c.expiresDate] forKey:@"expiresDate"];
[cookies setObject:d forKey:c.name];
[cookies setObject:c.value forKey:c.name];
}
resolve(cookies);
}
Expand Down