Skip to content

Commit 30fa70a

Browse files
committed
isMatchingDomain method added
1 parent 3895987 commit 30fa70a

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

macos/RNCookieManagerMacOs/RNCookieManagerMacOs.h

-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
* LICENSE file here: https://github.com/joeferraro/react-native-cookies/blob/master/LICENSE.md.
66
*/
77

8-
#if __has_include("RCTBridgeModule.h")
9-
#import "RCTBridgeModule.h"
10-
#else
118
#import <React/RCTBridgeModule.h>
12-
#endif
139

1410
#import <WebKit/WebKit.h>
1511

macos/RNCookieManagerMacOs/RNCookieManagerMacOs.m

+18-11
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66
*/
77

88
#import "RNCookieManagerMacOS.h"
9-
#if __has_include("RCTConvert.h")
10-
#import "RCTConvert.h"
11-
#else
129
#import <React/RCTConvert.h>
13-
#endif
1410

15-
static NSString * const NOT_AVAILABLE_ERROR_MESSAGE = @"WebKit/WebKit-Components are only available with macOS 10.2 and higher!";
11+
static NSString * const NOT_AVAILABLE_ERROR_MESSAGE = @"WebKit/WebKit-Components are only available with macOS 10.13 and higher!";
1612
static NSString * const INVALID_URL_MISSING_HTTP = @"Invalid URL: It may be missing a protocol (ex. http:// or https://).";
1713
static NSString * const INVALID_DOMAINS = @"Cookie URL host %@ and domain %@ mismatched. The cookie won't set correctly.";
1814

@@ -47,7 +43,8 @@ + (BOOL)requiresMainQueueSetup
4743
cookie:(NSDictionary *)props
4844
useWebKit:(BOOL)useWebKit
4945
resolver:(RCTPromiseResolveBlock)resolve
50-
rejecter:(RCTPromiseRejectBlock)reject) {
46+
rejecter:(RCTPromiseRejectBlock)reject)
47+
{
5148
NSHTTPCookie *cookie;
5249
@try {
5350
cookie = [self makeHTTPCookieObject:url props:props];
@@ -58,7 +55,7 @@ + (BOOL)requiresMainQueueSetup
5855
}
5956

6057
if (useWebKit) {
61-
if (@available(macOS 10.2, *)) {
58+
if (@available(macOS 10.13, *)) {
6259
dispatch_async(dispatch_get_main_queue(), ^(){
6360
WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
6461
[cookieStore setCookie:cookie completionHandler:^() {
@@ -113,7 +110,7 @@ + (BOOL)requiresMainQueueSetup
113110
rejecter:(RCTPromiseRejectBlock)reject)
114111
{
115112
if (useWebKit) {
116-
if (@available(macOS 10.2, *)) {
113+
if (@available(macOS 10.13, *)) {
117114
dispatch_async(dispatch_get_main_queue(), ^(){
118115
NSString *topLevelDomain = url.host;
119116

@@ -152,7 +149,7 @@ + (BOOL)requiresMainQueueSetup
152149
rejecter:(RCTPromiseRejectBlock)reject)
153150
{
154151
if (useWebKit) {
155-
if (@available(macOS 10.2, *)) {
152+
if (@available(macOS 10.13, *)) {
156153
dispatch_async(dispatch_get_main_queue(), ^(){
157154
// https://stackoverflow.com/questions/46465070/how-to-delete-cookies-from-wkhttpcookiestore#answer-47928399
158155
NSSet *websiteDataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeCookies]];
@@ -186,7 +183,7 @@ + (BOOL)requiresMainQueueSetup
186183
NSMutableArray<NSHTTPCookie *> * foundCookiesList = [NSMutableArray new];
187184

188185
if (useWebKit) {
189-
if (@available(macOS 10.2, *)) {
186+
if (@available(macOS 10.13, *)) {
190187
dispatch_async(dispatch_get_main_queue(), ^(){
191188
NSString *topLevelDomain = url.host;
192189

@@ -230,7 +227,7 @@ + (BOOL)requiresMainQueueSetup
230227
rejecter:(RCTPromiseRejectBlock)reject)
231228
{
232229
if (useWebKit) {
233-
if (@available(macOS 10.2, *)) {
230+
if (@available(macOS 10.13, *)) {
234231
dispatch_async(dispatch_get_main_queue(), ^(){
235232
WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
236233
[cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> *allCookies) {
@@ -340,4 +337,14 @@ -(NSDictionary *)createCookieData:(NSHTTPCookie *)cookie
340337
return cookieData;
341338
}
342339

340+
-(BOOL)isMatchingDomain:(NSString *)originDomain
341+
cookieDomain:(NSString *)cookieDomain
342+
{
343+
if ([originDomain isEqualToString: cookieDomain]) {
344+
return @YES;
345+
}
346+
NSString *parentDomain = [cookieDomain hasPrefix:@"."] ? cookieDomain : [@"." stringByAppendingString: cookieDomain];
347+
return [originDomain hasSuffix:parentDomain];
348+
}
349+
343350
@end

0 commit comments

Comments
 (0)