From 6f85408ec4ca431ba72ed3bf03e713a95ef3d2e8 Mon Sep 17 00:00:00 2001 From: "Ramo, Davide" <> Date: Fri, 22 Sep 2023 15:46:42 +0200 Subject: [PATCH] feat: iOS - add first request headers and cookies as options --- src/ios/CDVInAppBrowserOptions.h | 8 ++++- src/ios/CDVInAppBrowserOptions.m | 25 ++++++++++++++- src/ios/CDVWKInAppBrowser.h | 2 +- src/ios/CDVWKInAppBrowser.m | 55 +++++++++++++++++++++++++++++--- 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/src/ios/CDVInAppBrowserOptions.h b/src/ios/CDVInAppBrowserOptions.h index c1c9fa5..bffa7a0 100644 --- a/src/ios/CDVInAppBrowserOptions.h +++ b/src/ios/CDVInAppBrowserOptions.h @@ -17,7 +17,6 @@ under the License. */ - @interface CDVInAppBrowserOptions : NSObject {} @property (nonatomic, assign) BOOL location; @@ -45,6 +44,13 @@ @property (nonatomic, assign) BOOL disallowoverscroll; @property (nonatomic, copy) NSString* beforeload; +@property (nonatomic, copy) NSDictionary* headers; + +/** + Key is the cookie name, value is a Set-Cookie HTTP header string representation. + */ +@property (nonatomic, copy) NSDictionary* cookies; + + (CDVInAppBrowserOptions*)parseOptions:(NSString*)options; @end diff --git a/src/ios/CDVInAppBrowserOptions.m b/src/ios/CDVInAppBrowserOptions.m index e20d1a8..eb07102 100644 --- a/src/ios/CDVInAppBrowserOptions.m +++ b/src/ios/CDVInAppBrowserOptions.m @@ -45,6 +45,9 @@ self.toolbarcolor = nil; self.toolbartranslucent = YES; self.beforeload = @""; + + self.headers = @{}; + self.cookies = @{}; } return self; @@ -70,13 +73,33 @@ NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setAllowsFloats:YES]; BOOL isNumber = [numberFormatter numberFromString:value_lc] != nil; - + + //JSON values are base64 encoded + //We need to replace `=` to `@` cause `=` is used as key/value separator for options + //Cookies and headers are serialized in JSON and base64 encoding is need to include JSON into JS->Native options serialization. + BOOL isJson = false; + NSData *base64Value = [[NSData alloc] initWithBase64EncodedString:[value stringByReplacingOccurrencesOfString:@"@" withString:@"="] + options:NSDataBase64DecodingIgnoreUnknownCharacters]; + + NSError *error = nil; + id jsonValue = nil; + if(base64Value) { + jsonValue = [NSJSONSerialization JSONObjectWithData:base64Value + options:0 + error:&error]; + if(!error && jsonValue) { + isJson = true; + } + } + // set the property according to the key name if ([obj respondsToSelector:NSSelectorFromString(key)]) { if (isNumber) { [obj setValue:[numberFormatter numberFromString:value_lc] forKey:key]; } else if (isBoolean) { [obj setValue:[NSNumber numberWithBool:[value_lc isEqualToString:@"yes"]] forKey:key]; + } else if (isJson) { + [obj setValue:jsonValue forKey:key]; } else { [obj setValue:value forKey:key]; } diff --git a/src/ios/CDVWKInAppBrowser.h b/src/ios/CDVWKInAppBrowser.h index 2f650b8..792de5b 100644 --- a/src/ios/CDVWKInAppBrowser.h +++ b/src/ios/CDVWKInAppBrowser.h @@ -70,7 +70,7 @@ @property (nonatomic) NSURL* currentURL; - (void)close; -- (void)navigateTo:(NSURL*)url; +- (void)navigateTo:(NSURL*)url options:(CDVInAppBrowserOptions*)options; - (void)showLocationBar:(BOOL)show; - (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition; - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex; diff --git a/src/ios/CDVWKInAppBrowser.m b/src/ios/CDVWKInAppBrowser.m index 5cf8e15..8b956ba 100644 --- a/src/ios/CDVWKInAppBrowser.m +++ b/src/ios/CDVWKInAppBrowser.m @@ -199,6 +199,47 @@ static CDVWKInAppBrowser* instance = nil; NSLog(@"clearsessioncache not available below iOS 11.0"); } } + + if (browserOptions.cookies.count > 0) { + + bool isAtLeastiOS11 = false; +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 + if (@available(iOS 11.0, *)) { + isAtLeastiOS11 = true; + } +#endif + + if(isAtLeastiOS11){ +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 + + // Set all cookies + WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore; + for(NSString* key in browserOptions.cookies){ + + NSURL* url = [NSURL URLWithString:key]; + if(!url){ + NSLog(@"Cookie key is not a proper NSURL!"); + continue; + } + + NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:@{ @"Set-Cookie" : browserOptions.cookies[key] } forURL:url]; + + if(cookies.count == 0) { + NSLog(@"No cookies to process!"); + } + + for(NSHTTPCookie* cookie in cookies){ + [cookieStore setCookie:cookie completionHandler:nil]; + } + + } + +#endif + }else{ + NSLog(@"Cookies set is only available for iOS 11+!"); + } + + } if (self.inAppBrowserViewController == nil) { self.inAppBrowserViewController = [[CDVWKInAppBrowserViewController alloc] initWithBrowserOptions: browserOptions andSettings:self.commandDelegate.settings]; @@ -258,7 +299,7 @@ static CDVWKInAppBrowser* instance = nil; } _waitForBeforeload = ![_beforeload isEqualToString:@""]; - [self.inAppBrowserViewController navigateTo:url]; + [self.inAppBrowserViewController navigateTo:url options:browserOptions]; if (!browserOptions.hidden) { [self show:nil withNoAnimate:browserOptions.hidden]; } @@ -385,7 +426,7 @@ static CDVWKInAppBrowser* instance = nil; NSURL* url = [NSURL URLWithString:urlStr]; //_beforeload = @""; _waitForBeforeload = NO; - [self.inAppBrowserViewController navigateTo:url]; + [self.inAppBrowserViewController navigateTo:url options:nil]; } // This is a helper method for the inject{Script|Style}{Code|File} API calls, which @@ -1121,12 +1162,18 @@ BOOL isExiting = FALSE; }); } -- (void)navigateTo:(NSURL*)url +- (void)navigateTo:(NSURL*)url options:(CDVInAppBrowserOptions*)options { + NSLog(@"options headers: %@", options.headers); if ([url.scheme isEqualToString:@"file"]) { [self.webView loadFileURL:url allowingReadAccessToURL:url]; } else { - NSURLRequest* request = [NSURLRequest requestWithURL:url]; + NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; + for(NSString* key in options.headers) { + NSString* value = options.headers[key]; + [request setValue:value forHTTPHeaderField:key]; + } + NSLog(@"request headers: %@", request.allHTTPHeaderFields); [self.webView loadRequest:request]; } }