feat: iOS - add first request headers and cookies as options
This commit is contained in:
parent
d533f2a953
commit
6f85408ec4
@ -17,7 +17,6 @@
|
|||||||
under the License.
|
under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@interface CDVInAppBrowserOptions : NSObject {}
|
@interface CDVInAppBrowserOptions : NSObject {}
|
||||||
|
|
||||||
@property (nonatomic, assign) BOOL location;
|
@property (nonatomic, assign) BOOL location;
|
||||||
@ -45,6 +44,13 @@
|
|||||||
@property (nonatomic, assign) BOOL disallowoverscroll;
|
@property (nonatomic, assign) BOOL disallowoverscroll;
|
||||||
@property (nonatomic, copy) NSString* beforeload;
|
@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;
|
+ (CDVInAppBrowserOptions*)parseOptions:(NSString*)options;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -45,6 +45,9 @@
|
|||||||
self.toolbarcolor = nil;
|
self.toolbarcolor = nil;
|
||||||
self.toolbartranslucent = YES;
|
self.toolbartranslucent = YES;
|
||||||
self.beforeload = @"";
|
self.beforeload = @"";
|
||||||
|
|
||||||
|
self.headers = @{};
|
||||||
|
self.cookies = @{};
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
@ -70,13 +73,33 @@
|
|||||||
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
|
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
|
||||||
[numberFormatter setAllowsFloats:YES];
|
[numberFormatter setAllowsFloats:YES];
|
||||||
BOOL isNumber = [numberFormatter numberFromString:value_lc] != nil;
|
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
|
// set the property according to the key name
|
||||||
if ([obj respondsToSelector:NSSelectorFromString(key)]) {
|
if ([obj respondsToSelector:NSSelectorFromString(key)]) {
|
||||||
if (isNumber) {
|
if (isNumber) {
|
||||||
[obj setValue:[numberFormatter numberFromString:value_lc] forKey:key];
|
[obj setValue:[numberFormatter numberFromString:value_lc] forKey:key];
|
||||||
} else if (isBoolean) {
|
} else if (isBoolean) {
|
||||||
[obj setValue:[NSNumber numberWithBool:[value_lc isEqualToString:@"yes"]] forKey:key];
|
[obj setValue:[NSNumber numberWithBool:[value_lc isEqualToString:@"yes"]] forKey:key];
|
||||||
|
} else if (isJson) {
|
||||||
|
[obj setValue:jsonValue forKey:key];
|
||||||
} else {
|
} else {
|
||||||
[obj setValue:value forKey:key];
|
[obj setValue:value forKey:key];
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@
|
|||||||
@property (nonatomic) NSURL* currentURL;
|
@property (nonatomic) NSURL* currentURL;
|
||||||
|
|
||||||
- (void)close;
|
- (void)close;
|
||||||
- (void)navigateTo:(NSURL*)url;
|
- (void)navigateTo:(NSURL*)url options:(CDVInAppBrowserOptions*)options;
|
||||||
- (void)showLocationBar:(BOOL)show;
|
- (void)showLocationBar:(BOOL)show;
|
||||||
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
|
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
|
||||||
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex;
|
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex;
|
||||||
|
@ -199,6 +199,47 @@ static CDVWKInAppBrowser* instance = nil;
|
|||||||
NSLog(@"clearsessioncache not available below iOS 11.0");
|
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<NSHTTPCookie*> *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) {
|
if (self.inAppBrowserViewController == nil) {
|
||||||
self.inAppBrowserViewController = [[CDVWKInAppBrowserViewController alloc] initWithBrowserOptions: browserOptions andSettings:self.commandDelegate.settings];
|
self.inAppBrowserViewController = [[CDVWKInAppBrowserViewController alloc] initWithBrowserOptions: browserOptions andSettings:self.commandDelegate.settings];
|
||||||
@ -258,7 +299,7 @@ static CDVWKInAppBrowser* instance = nil;
|
|||||||
}
|
}
|
||||||
_waitForBeforeload = ![_beforeload isEqualToString:@""];
|
_waitForBeforeload = ![_beforeload isEqualToString:@""];
|
||||||
|
|
||||||
[self.inAppBrowserViewController navigateTo:url];
|
[self.inAppBrowserViewController navigateTo:url options:browserOptions];
|
||||||
if (!browserOptions.hidden) {
|
if (!browserOptions.hidden) {
|
||||||
[self show:nil withNoAnimate:browserOptions.hidden];
|
[self show:nil withNoAnimate:browserOptions.hidden];
|
||||||
}
|
}
|
||||||
@ -385,7 +426,7 @@ static CDVWKInAppBrowser* instance = nil;
|
|||||||
NSURL* url = [NSURL URLWithString:urlStr];
|
NSURL* url = [NSURL URLWithString:urlStr];
|
||||||
//_beforeload = @"";
|
//_beforeload = @"";
|
||||||
_waitForBeforeload = NO;
|
_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
|
// 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"]) {
|
if ([url.scheme isEqualToString:@"file"]) {
|
||||||
[self.webView loadFileURL:url allowingReadAccessToURL:url];
|
[self.webView loadFileURL:url allowingReadAccessToURL:url];
|
||||||
} else {
|
} 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];
|
[self.webView loadRequest:request];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user