Advertisement
SeriousVenom

CDVWKInAppBrowser.m

Nov 16th, 2020
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 52.96 KB | None | 0 0
  1. /*
  2.  Licensed to the Apache Software Foundation (ASF) under one
  3.  or more contributor license agreements.  See the NOTICE file
  4.  distributed with this work for additional information
  5.  regarding copyright ownership.  The ASF licenses this file
  6.  to you under the Apache License, Version 2.0 (the
  7.  "License"); you may not use this file except in compliance
  8.  with the License.  You may obtain a copy of the License at
  9.  
  10.  http://www.apache.org/licenses/LICENSE-2.0
  11.  
  12.  Unless required by applicable law or agreed to in writing,
  13.  software distributed under the License is distributed on an
  14.  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  KIND, either express or implied.  See the License for the
  16.  specific language governing permissions and limitations
  17.  under the License.
  18.  */
  19.  
  20. #import "CDVWKInAppBrowser.h"
  21.  
  22. #if __has_include("CDVWKProcessPoolFactory.h")
  23. #import "CDVWKProcessPoolFactory.h"
  24. #endif
  25.  
  26. #import <Cordova/CDVPluginResult.h>
  27.  
  28. #define    kInAppBrowserTargetSelf @"_self"
  29. #define    kInAppBrowserTargetSystem @"_system"
  30. #define    kInAppBrowserTargetBlank @"_blank"
  31.  
  32. #define    kInAppBrowserToolbarBarPositionBottom @"bottom"
  33. #define    kInAppBrowserToolbarBarPositionTop @"top"
  34.  
  35. #define    IAB_BRIDGE_NAME @"cordova_iab"
  36.  
  37. #define    TOOLBAR_HEIGHT 44.0
  38. #define    LOCATIONBAR_HEIGHT 21.0
  39. #define    FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT))
  40.  
  41. #pragma mark CDVWKInAppBrowser
  42.  
  43. @interface CDVWKInAppBrowser () {
  44.     NSInteger _previousStatusBarStyle;
  45. }
  46. @end
  47.  
  48. @implementation CDVWKInAppBrowser
  49.  
  50. static CDVWKInAppBrowser* instance = nil;
  51.  
  52. + (id) getInstance{
  53.     return instance;
  54. }
  55.  
  56. - (void)pluginInitialize
  57. {
  58.     instance = self;
  59.     _previousStatusBarStyle = -1;
  60.     _callbackIdPattern = nil;
  61.     _beforeload = @"";
  62.     _waitForBeforeload = NO;
  63. }
  64.  
  65. - (void)onReset
  66. {
  67.     [self close:nil];
  68. }
  69.  
  70. - (void)close:(CDVInvokedUrlCommand*)command
  71. {
  72.     if (self.inAppBrowserViewController == nil) {
  73.         NSLog(@"IAB.close() called but it was already closed.");
  74.         return;
  75.     }
  76.    
  77.     // Things are cleaned up in browserExit.
  78.     [self.inAppBrowserViewController close];
  79. }
  80.  
  81. - (BOOL) isSystemUrl:(NSURL*)url
  82. {
  83.     if ([[url host] isEqualToString:@"itunes.apple.com"]) {
  84.         return YES;
  85.     }
  86.    
  87.     return NO;
  88. }
  89.  
  90. - (void)open:(CDVInvokedUrlCommand*)command
  91. {
  92.     CDVPluginResult* pluginResult;
  93.    
  94.     NSString* url = [command argumentAtIndex:0];
  95.     NSString* target = [command argumentAtIndex:1 withDefault:kInAppBrowserTargetSelf];
  96.     NSString* options = [command argumentAtIndex:2 withDefault:@"" andClass:[NSString class]];
  97.    
  98.     self.callbackId = command.callbackId;
  99.    
  100.     if (url != nil) {
  101.         NSURL* baseUrl = [self.webViewEngine URL];
  102.         NSURL* absoluteUrl = [[NSURL URLWithString:url relativeToURL:baseUrl] absoluteURL];
  103.        
  104.         if ([self isSystemUrl:absoluteUrl]) {
  105.             target = kInAppBrowserTargetSystem;
  106.         }
  107.        
  108.         if ([target isEqualToString:kInAppBrowserTargetSelf]) {
  109.             [self openInCordovaWebView:absoluteUrl withOptions:options];
  110.         } else if ([target isEqualToString:kInAppBrowserTargetSystem]) {
  111.             [self openInSystem:absoluteUrl];
  112.         } else { // _blank or anything else
  113.             [self openInInAppBrowser:absoluteUrl withOptions:options];
  114.         }
  115.        
  116.         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  117.     } else {
  118.         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"incorrect number of arguments"];
  119.     }
  120.    
  121.     [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
  122.     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  123. }
  124.  
  125. - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
  126. {
  127.     CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions parseOptions:options];
  128.    
  129.     WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
  130.     if (browserOptions.cleardata) {
  131.        
  132.         NSDate* dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  133.         [dataStore removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:dateFrom completionHandler:^{
  134.             NSLog(@"Removed all WKWebView data");
  135.             self.inAppBrowserViewController.webView.configuration.processPool = [[WKProcessPool alloc] init]; // create new process pool to flush all data
  136.         }];
  137.     }
  138.    
  139.     if (browserOptions.clearcache) {
  140.         bool isAtLeastiOS11 = false;
  141. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  142.         if (@available(iOS 11.0, *)) {
  143.             isAtLeastiOS11 = true;
  144.         }
  145. #endif
  146.            
  147.         if(isAtLeastiOS11){
  148. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  149.             // Deletes all cookies
  150.             WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
  151.             [cookieStore getAllCookies:^(NSArray* cookies) {
  152.                 NSHTTPCookie* cookie;
  153.                 for(cookie in cookies){
  154.                     [cookieStore deleteCookie:cookie completionHandler:nil];
  155.                 }
  156.             }];
  157. #endif
  158.         }else{
  159.             // https://stackoverflow.com/a/31803708/777265
  160.             // Only deletes domain cookies (not session cookies)
  161.             [dataStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
  162.              completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {
  163.                  for (WKWebsiteDataRecord *record  in records){
  164.                      NSSet<NSString*>* dataTypes = record.dataTypes;
  165.                      if([dataTypes containsObject:WKWebsiteDataTypeCookies]){
  166.                          [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
  167.                                forDataRecords:@[record]
  168.                                completionHandler:^{}];
  169.                      }
  170.                  }
  171.              }];
  172.         }
  173.     }
  174.    
  175.     if (browserOptions.clearsessioncache) {
  176.         bool isAtLeastiOS11 = false;
  177. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  178.         if (@available(iOS 11.0, *)) {
  179.             isAtLeastiOS11 = true;
  180.         }
  181. #endif
  182.         if (isAtLeastiOS11) {
  183. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  184.             // Deletes session cookies
  185.             WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
  186.             [cookieStore getAllCookies:^(NSArray* cookies) {
  187.                 NSHTTPCookie* cookie;
  188.                 for(cookie in cookies){
  189.                     if(cookie.sessionOnly){
  190.                         [cookieStore deleteCookie:cookie completionHandler:nil];
  191.                     }
  192.                 }
  193.             }];
  194. #endif
  195.         }else{
  196.             NSLog(@"clearsessioncache not available below iOS 11.0");
  197.         }
  198.     }
  199.  
  200.     if (self.inAppBrowserViewController == nil) {
  201.         self.inAppBrowserViewController = [[CDVWKInAppBrowserViewController alloc] initWithBrowserOptions: browserOptions andSettings:self.commandDelegate.settings];
  202.         self.inAppBrowserViewController.navigationDelegate = self;
  203.        
  204.         if ([self.viewController conformsToProtocol:@protocol(CDVScreenOrientationDelegate)]) {
  205.             self.inAppBrowserViewController.orientationDelegate = (UIViewController <CDVScreenOrientationDelegate>*)self.viewController;
  206.         }
  207.     }
  208.    
  209.     [self.inAppBrowserViewController showLocationBar:browserOptions.location];
  210.     [self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
  211.     if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
  212.         int closeButtonIndex = browserOptions.lefttoright ? (browserOptions.hidenavigationbuttons ? 1 : 4) : 0;
  213.         [self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor :closeButtonIndex];
  214.     }
  215.     // Set Presentation Style
  216.     UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
  217.     if (browserOptions.presentationstyle != nil) {
  218.         if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"pagesheet"]) {
  219.             presentationStyle = UIModalPresentationPageSheet;
  220.         } else if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"formsheet"]) {
  221.             presentationStyle = UIModalPresentationFormSheet;
  222.         }
  223.     }
  224.     self.inAppBrowserViewController.modalPresentationStyle = presentationStyle;
  225.    
  226.     // Set Transition Style
  227.     UIModalTransitionStyle transitionStyle = UIModalTransitionStyleCoverVertical; // default
  228.     if (browserOptions.transitionstyle != nil) {
  229.         if ([[browserOptions.transitionstyle lowercaseString] isEqualToString:@"fliphorizontal"]) {
  230.             transitionStyle = UIModalTransitionStyleFlipHorizontal;
  231.         } else if ([[browserOptions.transitionstyle lowercaseString] isEqualToString:@"crossdissolve"]) {
  232.             transitionStyle = UIModalTransitionStyleCrossDissolve;
  233.         }
  234.     }
  235.     self.inAppBrowserViewController.modalTransitionStyle = transitionStyle;
  236.    
  237.     //prevent webView from bouncing
  238.     if (browserOptions.disallowoverscroll) {
  239.         if ([self.inAppBrowserViewController.webView respondsToSelector:@selector(scrollView)]) {
  240.             ((UIScrollView*)[self.inAppBrowserViewController.webView scrollView]).bounces = NO;
  241.         } else {
  242.             for (id subview in self.inAppBrowserViewController.webView.subviews) {
  243.                 if ([[subview class] isSubclassOfClass:[UIScrollView class]]) {
  244.                     ((UIScrollView*)subview).bounces = NO;
  245.                 }
  246.             }
  247.         }
  248.     }
  249.    
  250.     // use of beforeload event
  251.     if([browserOptions.beforeload isKindOfClass:[NSString class]]){
  252.         _beforeload = browserOptions.beforeload;
  253.     }else{
  254.         _beforeload = @"yes";
  255.     }
  256.     _waitForBeforeload = ![_beforeload isEqualToString:@""];
  257.    
  258.     [self.inAppBrowserViewController navigateTo:url];
  259.     if (!browserOptions.hidden) {
  260.         [self show:nil withNoAnimate:browserOptions.hidden];
  261.     }
  262. }
  263.  
  264. - (void)show:(CDVInvokedUrlCommand*)command{
  265.     [self show:command withNoAnimate:NO];
  266. }
  267.  
  268. - (void)show:(CDVInvokedUrlCommand*)command withNoAnimate:(BOOL)noAnimate
  269. {
  270.     BOOL initHidden = NO;
  271.     if(command == nil && noAnimate == YES){
  272.         initHidden = YES;
  273.     }
  274.    
  275.     if (self.inAppBrowserViewController == nil) {
  276.         NSLog(@"Tried to show IAB after it was closed.");
  277.         return;
  278.     }
  279.     if (_previousStatusBarStyle != -1) {
  280.         NSLog(@"Tried to show IAB while already shown");
  281.         return;
  282.     }
  283.    
  284.     if(!initHidden){
  285.         _previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
  286.     }
  287.    
  288.     __block CDVInAppBrowserNavigationController* nav = [[CDVInAppBrowserNavigationController alloc]
  289.                                                         initWithRootViewController:self.inAppBrowserViewController];
  290.     nav.orientationDelegate = self.inAppBrowserViewController;
  291.     nav.navigationBarHidden = YES;
  292.     nav.modalPresentationStyle = self.inAppBrowserViewController.modalPresentationStyle;
  293.    
  294.     __weak CDVWKInAppBrowser* weakSelf = self;
  295.    
  296.     // Run later to avoid the "took a long time" log message.
  297.     dispatch_async(dispatch_get_main_queue(), ^{
  298.         if (weakSelf.inAppBrowserViewController != nil) {
  299.             float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
  300.             __strong __typeof(weakSelf) strongSelf = weakSelf;
  301.             if (!strongSelf->tmpWindow) {
  302.                 CGRect frame = [[UIScreen mainScreen] bounds];
  303.                 if(initHidden && osVersion < 11){
  304.                    frame.origin.x = -10000;
  305.                 }
  306.                 strongSelf->tmpWindow = [[UIWindow alloc] initWithFrame:frame];
  307.             }
  308.             UIViewController *tmpController = [[UIViewController alloc] init];
  309.  
  310.             [strongSelf->tmpWindow setRootViewController:tmpController];
  311.             [strongSelf->tmpWindow setWindowLevel:UIWindowLevelNormal];
  312.  
  313.             if(!initHidden || osVersion < 11){
  314.                 [self->tmpWindow makeKeyAndVisible];
  315.             }
  316.             [tmpController presentViewController:nav animated:!noAnimate completion:nil];
  317.         }
  318.     });
  319. }
  320.  
  321. - (void)hide:(CDVInvokedUrlCommand*)command
  322. {
  323.     // Set tmpWindow to hidden to make main webview responsive to touch again
  324.     // https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
  325.     self->tmpWindow.hidden = YES;
  326.     self->tmpWindow = nil;
  327.  
  328.     if (self.inAppBrowserViewController == nil) {
  329.         NSLog(@"Tried to hide IAB after it was closed.");
  330.         return;
  331.        
  332.        
  333.     }
  334.     if (_previousStatusBarStyle == -1) {
  335.         NSLog(@"Tried to hide IAB while already hidden");
  336.         return;
  337.     }
  338.    
  339.     _previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
  340.    
  341.     // Run later to avoid the "took a long time" log message.
  342.     dispatch_async(dispatch_get_main_queue(), ^{
  343.         if (self.inAppBrowserViewController != nil) {
  344.             _previousStatusBarStyle = -1;
  345.             [self.inAppBrowserViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  346.         }
  347.     });
  348. }
  349.  
  350. - (void)openInCordovaWebView:(NSURL*)url withOptions:(NSString*)options
  351. {
  352.     NSURLRequest* request = [NSURLRequest requestWithURL:url];
  353.     // the webview engine itself will filter for this according to <allow-navigation> policy
  354.     // in config.xml for cordova-ios-4.0
  355.     [self.webViewEngine loadRequest:request];
  356. }
  357.  
  358. - (void)openInSystem:(NSURL*)url
  359. {
  360.     if ([[UIApplication sharedApplication] openURL:url] == NO) {
  361.         [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
  362.         [[UIApplication sharedApplication] openURL:url];
  363.     }
  364. }
  365.  
  366. - (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
  367. {
  368.     NSString* urlStr = [command argumentAtIndex:0];
  369.  
  370.     if ([_beforeload isEqualToString:@""]) {
  371.         NSLog(@"unexpected loadAfterBeforeload called without feature beforeload=get|post");
  372.     }
  373.     if (self.inAppBrowserViewController == nil) {
  374.         NSLog(@"Tried to invoke loadAfterBeforeload on IAB after it was closed.");
  375.         return;
  376.     }
  377.     if (urlStr == nil) {
  378.         NSLog(@"loadAfterBeforeload called with nil argument, ignoring.");
  379.         return;
  380.     }
  381.  
  382.     NSURL* url = [NSURL URLWithString:urlStr];
  383.     //_beforeload = @"";
  384.     _waitForBeforeload = NO;
  385.     [self.inAppBrowserViewController navigateTo:url];
  386. }
  387.  
  388. // This is a helper method for the inject{Script|Style}{Code|File} API calls, which
  389. // provides a consistent method for injecting JavaScript code into the document.
  390. //
  391. // If a wrapper string is supplied, then the source string will be JSON-encoded (adding
  392. // quotes) and wrapped using string formatting. (The wrapper string should have a single
  393. // '%@' marker).
  394. //
  395. // If no wrapper is supplied, then the source string is executed directly.
  396.  
  397. - (void)injectDeferredObject:(NSString*)source withWrapper:(NSString*)jsWrapper
  398. {
  399.     // Ensure a message handler bridge is created to communicate with the CDVWKInAppBrowserViewController
  400.     [self evaluateJavaScript: [NSString stringWithFormat:@"(function(w){if(!w._cdvMessageHandler) {w._cdvMessageHandler = function(id,d){w.webkit.messageHandlers.%@.postMessage({d:d, id:id});}}})(window)", IAB_BRIDGE_NAME]];
  401.    
  402.     if (jsWrapper != nil) {
  403.         NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@[source] options:0 error:nil];
  404.         NSString* sourceArrayString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  405.         if (sourceArrayString) {
  406.             NSString* sourceString = [sourceArrayString substringWithRange:NSMakeRange(1, [sourceArrayString length] - 2)];
  407.             NSString* jsToInject = [NSString stringWithFormat:jsWrapper, sourceString];
  408.             [self evaluateJavaScript:jsToInject];
  409.         }
  410.     } else {
  411.         [self evaluateJavaScript:source];
  412.     }
  413. }
  414.  
  415.  
  416. //Synchronus helper for javascript evaluation
  417. - (void)evaluateJavaScript:(NSString *)script {
  418.     __block NSString* _script = script;
  419.     [self.inAppBrowserViewController.webView evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
  420.         if (error == nil) {
  421.             if (result != nil) {
  422.                 NSLog(@"%@", result);
  423.             }
  424.         } else {
  425.             NSLog(@"evaluateJavaScript error : %@ : %@", error.localizedDescription, _script);
  426.         }
  427.     }];
  428. }
  429.  
  430. - (void)injectScriptCode:(CDVInvokedUrlCommand*)command
  431. {
  432.     NSString* jsWrapper = nil;
  433.    
  434.     if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
  435.         jsWrapper = [NSString stringWithFormat:@"_cdvMessageHandler('%@',JSON.stringify([eval(%%@)]));", command.callbackId];
  436.     }
  437.     [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
  438. }
  439.  
  440. - (void)injectScriptFile:(CDVInvokedUrlCommand*)command
  441. {
  442.     NSString* jsWrapper;
  443.    
  444.     if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
  445.         jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('script'); c.src = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
  446.     } else {
  447.         jsWrapper = @"(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document)";
  448.     }
  449.     [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
  450. }
  451.  
  452. - (void)injectStyleCode:(CDVInvokedUrlCommand*)command
  453. {
  454.     NSString* jsWrapper;
  455.    
  456.     if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
  457.         jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('style'); c.innerHTML = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
  458.     } else {
  459.         jsWrapper = @"(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document)";
  460.     }
  461.     [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
  462. }
  463.  
  464. - (void)injectStyleFile:(CDVInvokedUrlCommand*)command
  465. {
  466.     NSString* jsWrapper;
  467.    
  468.     if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
  469.         jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
  470.     } else {
  471.         jsWrapper = @"(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document)";
  472.     }
  473.     [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
  474. }
  475.  
  476. - (BOOL)isValidCallbackId:(NSString *)callbackId
  477. {
  478.     NSError *err = nil;
  479.     // Initialize on first use
  480.     if (self.callbackIdPattern == nil) {
  481.         self.callbackIdPattern = [NSRegularExpression regularExpressionWithPattern:@"^InAppBrowser[0-9]{1,10}$" options:0 error:&err];
  482.         if (err != nil) {
  483.             // Couldn't initialize Regex; No is safer than Yes.
  484.             return NO;
  485.         }
  486.     }
  487.     if ([self.callbackIdPattern firstMatchInString:callbackId options:0 range:NSMakeRange(0, [callbackId length])]) {
  488.         return YES;
  489.     }
  490.     return NO;
  491. }
  492.  
  493. /**
  494.  * The message handler bridge provided for the InAppBrowser is capable of executing any oustanding callback belonging
  495.  * to the InAppBrowser plugin. Care has been taken that other callbacks cannot be triggered, and that no
  496.  * other code execution is possible.
  497.  */
  498. - (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  499.    
  500.     NSURL* url = navigationAction.request.URL;
  501.     NSURL* mainDocumentURL = navigationAction.request.mainDocumentURL;
  502.     BOOL isTopLevelNavigation = [url isEqual:mainDocumentURL];
  503.     BOOL shouldStart = YES;
  504.     BOOL useBeforeLoad = NO;
  505.     NSString* httpMethod = navigationAction.request.HTTPMethod;
  506.     NSString* errorMessage = nil;
  507.    
  508.     if([_beforeload isEqualToString:@"post"]){
  509.         //TODO handle POST requests by preserving POST data then remove this condition
  510.         errorMessage = @"beforeload doesn't yet support POST requests";
  511.     }
  512.     else if(isTopLevelNavigation && (
  513.            [_beforeload isEqualToString:@"yes"]
  514.        || ([_beforeload isEqualToString:@"get"] && [httpMethod isEqualToString:@"GET"])
  515.     // TODO comment in when POST requests are handled
  516.     // || ([_beforeload isEqualToString:@"post"] && [httpMethod isEqualToString:@"POST"])
  517.     )){
  518.         useBeforeLoad = YES;
  519.     }
  520.  
  521.     // When beforeload, on first URL change, initiate JS callback. Only after the beforeload event, continue.
  522.     if (_waitForBeforeload && useBeforeLoad) {
  523.         CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  524.                                                       messageAsDictionary:@{@"type":@"beforeload", @"url":[url absoluteString]}];
  525.         [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
  526.        
  527.         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
  528.         decisionHandler(WKNavigationActionPolicyCancel);
  529.         return;
  530.     }
  531.    
  532.     if(errorMessage != nil){
  533.         NSLog(errorMessage);
  534.         CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
  535.                                                       messageAsDictionary:@{@"type":@"loaderror", @"url":[url absoluteString], @"code": @"-1", @"message": errorMessage}];
  536.         [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
  537.         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
  538.     }
  539.    
  540.     //if is an app store link, let the system handle it, otherwise it fails to load it
  541.     if ([[ url scheme] isEqualToString:@"itms-appss"] || [[ url scheme] isEqualToString:@"itms-apps"]) {
  542.         [theWebView stopLoading];
  543.         [self openInSystem:url];
  544.         shouldStart = NO;
  545.     }
  546.     else if ((self.callbackId != nil) && isTopLevelNavigation) {
  547.         // Send a loadstart event for each top-level navigation (includes redirects).
  548.         CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  549.                                                       messageAsDictionary:@{@"type":@"loadstart", @"url":[url absoluteString]}];
  550.         [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
  551.        
  552.         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
  553.     }
  554.  
  555.     if (useBeforeLoad) {
  556.         _waitForBeforeload = YES;
  557.     }
  558.    
  559.     if(shouldStart){
  560.         // Fix GH-417 & GH-424: Handle non-default target attribute
  561.         // Based on https://stackoverflow.com/a/25713070/777265
  562.         if (!navigationAction.targetFrame){
  563.             [theWebView loadRequest:navigationAction.request];
  564.             decisionHandler(WKNavigationActionPolicyCancel);
  565.         }else{
  566.             decisionHandler(WKNavigationActionPolicyAllow);
  567.         }
  568.     }else{
  569.         decisionHandler(WKNavigationActionPolicyCancel);
  570.     }
  571. }
  572.  
  573. #pragma mark WKScriptMessageHandler delegate
  574. - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
  575.    
  576.     CDVPluginResult* pluginResult = nil;
  577.    
  578.     if([message.body isKindOfClass:[NSDictionary class]]){
  579.         NSDictionary* messageContent = (NSDictionary*) message.body;
  580.         NSString* scriptCallbackId = messageContent[@"id"];
  581.        
  582.         if([messageContent objectForKey:@"d"]){
  583.             NSString* scriptResult = messageContent[@"d"];
  584.             NSError* __autoreleasing error = nil;
  585.             NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
  586.             if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
  587.                 pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
  588.             } else {
  589.                 pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
  590.             }
  591.         } else {
  592.             pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
  593.         }
  594.         [self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
  595.     }else if(self.callbackId != nil){
  596.         // Send a message event
  597.         NSString* messageContent = (NSString*) message.body;
  598.         NSError* __autoreleasing error = nil;
  599.         NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[messageContent dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
  600.         if (error == nil) {
  601.             NSMutableDictionary* dResult = [NSMutableDictionary new];
  602.             [dResult setValue:@"message" forKey:@"type"];
  603.             [dResult setObject:decodedResult forKey:@"data"];
  604.             CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dResult];
  605.             [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
  606.             [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
  607.         }
  608.     }
  609. }
  610.  
  611. - (void)didStartProvisionalNavigation:(WKWebView*)theWebView
  612. {
  613.     NSLog(@"didStartProvisionalNavigation");
  614. //    self.inAppBrowserViewController.currentURL = theWebView.URL;
  615. }
  616.  
  617. - (void)didFinishNavigation:(WKWebView*)theWebView
  618. {
  619.     if (self.callbackId != nil) {
  620.         NSString* url = [theWebView.URL absoluteString];
  621.         if(url == nil){
  622.             if(self.inAppBrowserViewController.currentURL != nil){
  623.                 url = [self.inAppBrowserViewController.currentURL absoluteString];
  624.             }else{
  625.                 url = @"";
  626.             }
  627.         }
  628.         CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  629.                                                       messageAsDictionary:@{@"type":@"loadstop", @"url":url}];
  630.         [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
  631.        
  632.         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
  633.     }
  634. }
  635.  
  636. - (void)webView:(WKWebView*)theWebView didFailNavigation:(NSError*)error
  637. {
  638.     if (self.callbackId != nil) {
  639.         NSString* url = [theWebView.URL absoluteString];
  640.         if(url == nil){
  641.             if(self.inAppBrowserViewController.currentURL != nil){
  642.                 url = [self.inAppBrowserViewController.currentURL absoluteString];
  643.             }else{
  644.                 url = @"";
  645.             }
  646.         }
  647.         CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
  648.                                                       messageAsDictionary:@{@"type":@"loaderror", @"url":url, @"code": [NSNumber numberWithInteger:error.code], @"message": error.localizedDescription}];
  649.         [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
  650.        
  651.         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
  652.     }
  653. }
  654.  
  655. - (void)browserExit
  656. {
  657.     if (self.callbackId != nil) {
  658.         CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  659.                                                       messageAsDictionary:@{@"type":@"exit"}];
  660.         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
  661.         self.callbackId = nil;
  662.     }
  663.    
  664.     [self.inAppBrowserViewController.configuration.userContentController removeScriptMessageHandlerForName:IAB_BRIDGE_NAME];
  665.     self.inAppBrowserViewController.configuration = nil;
  666.    
  667.     [self.inAppBrowserViewController.webView stopLoading];
  668.     [self.inAppBrowserViewController.webView removeFromSuperview];
  669.     [self.inAppBrowserViewController.webView setUIDelegate:nil];
  670.     [self.inAppBrowserViewController.webView setNavigationDelegate:nil];
  671.     self.inAppBrowserViewController.webView = nil;
  672.    
  673.     // Set navigationDelegate to nil to ensure no callbacks are received from it.
  674.     self.inAppBrowserViewController.navigationDelegate = nil;
  675.     self.inAppBrowserViewController = nil;
  676.  
  677.     // Set tmpWindow to hidden to make main webview responsive to touch again
  678.     // Based on https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
  679.     self->tmpWindow.hidden = YES;
  680.     self->tmpWindow = nil;
  681.  
  682.     if (IsAtLeastiOSVersion(@"7.0")) {
  683.         if (_previousStatusBarStyle != -1) {
  684.             [[UIApplication sharedApplication] setStatusBarStyle:_previousStatusBarStyle];
  685.            
  686.         }
  687.     }
  688.    
  689.     _previousStatusBarStyle = -1; // this value was reset before reapplying it. caused statusbar to stay black on ios7
  690. }
  691.  
  692. @end //CDVWKInAppBrowser
  693.  
  694. #pragma mark CDVWKInAppBrowserViewController
  695.  
  696. @implementation CDVWKInAppBrowserViewController
  697.  
  698. @synthesize currentURL;
  699.  
  700. CGFloat lastReducedStatusBarHeight = 0.0;
  701. BOOL isExiting = FALSE;
  702.  
  703. - (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary *)settings
  704. {
  705.     self = [super init];
  706.     if (self != nil) {
  707.         _browserOptions = browserOptions;
  708.         _settings = settings;
  709.         self.webViewUIDelegate = [[CDVWKInAppBrowserUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
  710.         [self.webViewUIDelegate setViewController:self];
  711.        
  712.         [self createViews];
  713.     }
  714.    
  715.     return self;
  716. }
  717.  
  718. -(void)dealloc {
  719.     //NSLog(@"dealloc");
  720. }
  721.  
  722. - (void)createViews
  723. {
  724.     // We create the views in code for primarily for ease of upgrades and not requiring an external .xib to be included
  725.    
  726.     CGRect webViewBounds = self.view.bounds;
  727.     BOOL toolbarIsAtBottom = ![_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop];
  728.     webViewBounds.size.height -= _browserOptions.location ? FOOTER_HEIGHT : TOOLBAR_HEIGHT;
  729.     WKUserContentController* userContentController = [[WKUserContentController alloc] init];
  730.    
  731.     WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
  732.    
  733.     NSString *userAgent = configuration.applicationNameForUserAgent;
  734.     if (
  735.         [self settingForKey:@"OverrideUserAgent"] == nil &&
  736.         [self settingForKey:@"AppendUserAgent"] != nil
  737.         ) {
  738.         userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [self settingForKey:@"AppendUserAgent"]];
  739.     }
  740.     configuration.applicationNameForUserAgent = userAgent;
  741.     configuration.userContentController = userContentController;
  742. #if __has_include("CDVWKProcessPoolFactory.h")
  743.     configuration.processPool = [[CDVWKProcessPoolFactory sharedFactory] sharedProcessPool];
  744. #endif
  745.     [configuration.userContentController addScriptMessageHandler:self name:IAB_BRIDGE_NAME];
  746.    
  747.     //WKWebView options
  748.     configuration.allowsInlineMediaPlayback = _browserOptions.allowinlinemediaplayback;
  749.     if (IsAtLeastiOSVersion(@"10.0")) {
  750.         configuration.ignoresViewportScaleLimits = _browserOptions.enableviewportscale;
  751.         if(_browserOptions.mediaplaybackrequiresuseraction == YES){
  752.             configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
  753.         }else{
  754.             configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
  755.         }
  756.     }else{ // iOS 9
  757.         configuration.mediaPlaybackRequiresUserAction = _browserOptions.mediaplaybackrequiresuseraction;
  758.     }
  759.    
  760.    
  761.  
  762.     self.webView = [[WKWebView alloc] initWithFrame:webViewBounds configuration:configuration];
  763.    
  764.     [self.view addSubview:self.webView];
  765.     [self.view sendSubviewToBack:self.webView];
  766.    
  767.    
  768.     self.webView.navigationDelegate = self;
  769.     self.webView.UIDelegate = self.webViewUIDelegate;
  770.     self.webView.backgroundColor = [UIColor blackColor];
  771.     if ([self settingForKey:@"OverrideUserAgent"] != nil) {
  772.         self.webView.customUserAgent = [self settingForKey:@"OverrideUserAgent"];
  773.     }
  774.    
  775.     self.webView.clearsContextBeforeDrawing = YES;
  776.     self.webView.clipsToBounds = YES;
  777.     self.webView.contentMode = UIViewContentModeScaleToFill;
  778.     self.webView.multipleTouchEnabled = YES;
  779.     self.webView.opaque = YES;
  780.     self.webView.userInteractionEnabled = YES;
  781.     self.automaticallyAdjustsScrollViewInsets = YES ;
  782.     [self.webView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
  783.     self.webView.allowsLinkPreview = NO;
  784.     self.webView.allowsBackForwardNavigationGestures = NO;
  785.    
  786. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  787.    if (@available(iOS 11.0, *)) {
  788.        [self.webView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
  789.    }
  790. #endif
  791.    
  792.     self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  793.     self.spinner.alpha = 1.000;
  794.     self.spinner.autoresizesSubviews = YES;
  795.     self.spinner.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin);
  796.     self.spinner.clearsContextBeforeDrawing = NO;
  797.     self.spinner.clipsToBounds = NO;
  798.     self.spinner.contentMode = UIViewContentModeScaleToFill;
  799.     self.spinner.frame = CGRectMake(CGRectGetMidX(self.webView.frame), CGRectGetMidY(self.webView.frame), 20.0, 20.0);
  800.     self.spinner.hidden = NO;
  801.     self.spinner.hidesWhenStopped = YES;
  802.     self.spinner.multipleTouchEnabled = NO;
  803.     self.spinner.opaque = NO;
  804.     self.spinner.userInteractionEnabled = NO;
  805.     [self.spinner stopAnimating];
  806.    
  807.     self.closeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
  808.     self.closeButton.enabled = YES;
  809.    
  810.     UIBarButtonItem* flexibleSpaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  811.    
  812.     UIBarButtonItem* fixedSpaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  813.     fixedSpaceButton.width = 20;
  814.    
  815.     float toolbarY = toolbarIsAtBottom ? self.view.bounds.size.height - TOOLBAR_HEIGHT : 0.0;
  816.     CGRect toolbarFrame = CGRectMake(0.0, toolbarY, self.view.bounds.size.width, TOOLBAR_HEIGHT);
  817.    
  818.     self.toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
  819.     self.toolbar.alpha = 1.000;
  820.     self.toolbar.autoresizesSubviews = NO;
  821.     self.toolbar.autoresizingMask = toolbarIsAtBottom ? (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin) : UIViewAutoresizingFlexibleWidth;
  822.     self.toolbar.barStyle = UIBarStyleBlackOpaque;
  823.     self.toolbar.clearsContextBeforeDrawing = NO;
  824.     self.toolbar.clipsToBounds = NO;
  825.     self.toolbar.contentMode = UIViewContentModeScaleToFill;
  826.     self.toolbar.hidden = YES;
  827.     self.toolbar.multipleTouchEnabled = NO;
  828.     self.toolbar.opaque = NO;
  829.     self.toolbar.userInteractionEnabled = YES;
  830.     if (_browserOptions.toolbarcolor != nil) { // Set toolbar color if user sets it in options
  831.       self.toolbar.barTintColor = [self colorFromHexString:_browserOptions.toolbarcolor];
  832.     }
  833.     if (!_browserOptions.toolbartranslucent) { // Set toolbar translucent to no if user sets it in options
  834.       self.toolbar.translucent = NO;
  835.     }
  836.    
  837.     CGFloat labelInset = 5.0;
  838.     float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT;
  839.    
  840.     self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelInset, locationBarY, self.view.bounds.size.width - labelInset, LOCATIONBAR_HEIGHT)];
  841.     self.addressLabel.adjustsFontSizeToFitWidth = NO;
  842.     self.addressLabel.alpha = 1.000;
  843.     self.addressLabel.autoresizesSubviews = YES;
  844.     self.addressLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  845.     self.addressLabel.backgroundColor = [UIColor colorWithRed:255 green:223 blue:0 alpha:1.0];
  846.     self.addressLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
  847.     self.addressLabel.clearsContextBeforeDrawing = YES;
  848.     self.addressLabel.clipsToBounds = YES;
  849.     self.addressLabel.contentMode = UIViewContentModeScaleToFill;
  850.     self.addressLabel.enabled = YES;
  851.     self.addressLabel.hidden = NO;
  852.     self.addressLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  853.    
  854.     if ([self.addressLabel respondsToSelector:NSSelectorFromString(@"setMinimumScaleFactor:")]) {
  855.         [self.addressLabel setValue:@(10.0/[UIFont labelFontSize]) forKey:@"minimumScaleFactor"];
  856.     } else if ([self.addressLabel respondsToSelector:NSSelectorFromString(@"setMinimumFontSize:")]) {
  857.         [self.addressLabel setValue:@(10.0) forKey:@"minimumFontSize"];
  858.     }
  859.    
  860.     self.addressLabel.multipleTouchEnabled = NO;
  861.     self.addressLabel.numberOfLines = 1;
  862.     self.addressLabel.opaque = NO;
  863.     self.addressLabel.shadowOffset = CGSizeMake(0.0, -1.0);
  864.     self.addressLabel.text = NSLocalizedString(@"Loading...", nil);
  865.     self.addressLabel.textAlignment = NSTextAlignmentLeft;
  866.     self.addressLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.000];
  867.     self.addressLabel.userInteractionEnabled = NO;
  868.    
  869.     NSString* frontArrowString = NSLocalizedString(@"►", nil); // create arrow from Unicode char
  870.     self.forwardButton = [[UIBarButtonItem alloc] initWithTitle:frontArrowString style:UIBarButtonItemStylePlain target:self action:@selector(goForward:)];
  871.     self.forwardButton.enabled = YES;
  872.     self.forwardButton.imageInsets = UIEdgeInsetsZero;
  873.     if (_browserOptions.navigationbuttoncolor != nil) { // Set button color if user sets it in options
  874.       self.forwardButton.tintColor = [self colorFromHexString:_browserOptions.navigationbuttoncolor];
  875.     }
  876.  
  877.     NSString* backArrowString = NSLocalizedString(@"◄", nil); // create arrow from Unicode char
  878.     self.backButton = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
  879.     self.backButton.enabled = YES;
  880.     self.backButton.imageInsets = UIEdgeInsetsZero;
  881.     if (_browserOptions.navigationbuttoncolor != nil) { // Set button color if user sets it in options
  882.       self.backButton.tintColor = [self colorFromHexString:_browserOptions.navigationbuttoncolor];
  883.     }
  884.  
  885.     // Filter out Navigation Buttons if user requests so
  886.    if (_browserOptions.hidenavigationbuttons) {
  887.         if (_browserOptions.lefttoright) {
  888.             [self.toolbar setItems:@[flexibleSpaceButton, self.closeButton]];
  889.         } else {
  890.             [self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
  891.         }
  892.     } else if (_browserOptions.lefttoright) {
  893.         [self.toolbar setItems:@[self.backButton, fixedSpaceButton, self.forwardButton, flexibleSpaceButton, self.closeButton]];
  894.     } else {
  895.         //[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
  896.             [self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
  897.     }
  898.    
  899.   // self.view.backgroundColor = [UIColor clearColor];
  900.    // [self.view addSubview:self.toolbar];
  901.    // [self.view addSubview:self.addressLabel];
  902.    // [self.view addSubview:self.spinner];
  903. }
  904.  
  905. - (id)settingForKey:(NSString*)key
  906. {
  907.     return [_settings objectForKey:[key lowercaseString]];
  908. }
  909.  
  910. - (void) setWebViewFrame : (CGRect) frame {
  911.     NSLog(@"Setting the WebView's frame to %@", NSStringFromCGRect(frame));
  912.     [self.webView setFrame:frame];
  913. }
  914.  
  915. - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex
  916. {
  917.     // the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
  918.     // but, if you want to set this yourself, knock yourself out (we can't set the title for a system Done button, so we have to create a new one)
  919.     self.closeButton = nil;
  920.     // Initialize with title if title is set, otherwise the title will be 'Done' localized
  921.     self.closeButton = title != nil ? [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:@selector(close)] : [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
  922.     self.closeButton.enabled = YES;
  923.     // If color on closebutton is requested then initialize with that that color, otherwise use initialize with default
  924.     self.closeButton.tintColor = colorString != nil ? [self colorFromHexString:colorString] : [UIColor colorWithRed:60.0 / 255.0 green:136.0 / 255.0 blue:230.0 / 255.0 alpha:1];
  925.    
  926.     NSMutableArray* items = [self.toolbar.items mutableCopy];
  927.     [items replaceObjectAtIndex:buttonIndex withObject:self.closeButton];
  928.     [self.toolbar setItems:items];
  929. }
  930.  
  931. - (void)showLocationBar:(BOOL)show
  932. {
  933.     CGRect locationbarFrame = self.addressLabel.frame;
  934.    
  935.     BOOL toolbarVisible = !self.toolbar.hidden;
  936.    
  937.     // prevent double show/hide
  938.     if (show == !(self.addressLabel.hidden)) {
  939.         return;
  940.     }
  941.    
  942.     if (show) {
  943.         self.addressLabel.hidden = NO;
  944.        
  945.         if (toolbarVisible) {
  946.             // toolBar at the bottom, leave as is
  947.             // put locationBar on top of the toolBar
  948.            
  949.            CGRect webViewBounds = self.view.bounds;
  950.            webViewBounds.size.height -= FOOTER_HEIGHT;
  951.            [self setWebViewFrame:webViewBounds];
  952.            
  953.             locationbarFrame.origin.y = webViewBounds.size.height;
  954.             self.addressLabel.frame = locationbarFrame;
  955.         } else {
  956.             // no toolBar, so put locationBar at the bottom
  957.            
  958.             CGRect webViewBounds = self.view.bounds;
  959.             webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
  960.             [self setWebViewFrame:webViewBounds];
  961.            
  962.             locationbarFrame.origin.y = webViewBounds.size.height;
  963.             self.addressLabel.frame = locationbarFrame;
  964.         }
  965.     } else {
  966.         self.addressLabel.hidden = NO;
  967.        
  968.         if (toolbarVisible) {
  969.             // locationBar is on top of toolBar, hide locationBar
  970.            
  971.             // webView take up whole height less toolBar height
  972.             CGRect webViewBounds = self.view.bounds;
  973.             webViewBounds.size.height -= TOOLBAR_HEIGHT;
  974.             [self setWebViewFrame:webViewBounds];
  975.         } else {
  976.             // no toolBar, expand webView to screen dimensions
  977.             [self setWebViewFrame:self.view.bounds];
  978.         }
  979.     }
  980. }
  981.  
  982. - (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition
  983. {
  984.     CGRect toolbarFrame = self.toolbar.frame;
  985.     CGRect locationbarFrame = self.addressLabel.frame;
  986.    
  987.     BOOL locationbarVisible = !self.addressLabel.hidden;
  988.    
  989.     // prevent double show/hide
  990.     if (show == !(self.toolbar.hidden)) {
  991.         return;
  992.     }
  993.    
  994.     if (show) {
  995.         self.toolbar.hidden = NO;
  996.         CGRect webViewBounds = self.view.bounds;
  997.        
  998.         if (locationbarVisible) {
  999.             // locationBar at the bottom, move locationBar up
  1000.             // put toolBar at the bottom
  1001.             webViewBounds.size.height -= FOOTER_HEIGHT;
  1002.             locationbarFrame.origin.y = webViewBounds.size.height;
  1003.             self.addressLabel.frame = locationbarFrame;
  1004.             self.toolbar.frame = toolbarFrame;
  1005.         } else {
  1006.             // no locationBar, so put toolBar at the bottom
  1007.             CGRect webViewBounds = self.view.bounds;
  1008.             webViewBounds.size.height -= TOOLBAR_HEIGHT;
  1009.             self.toolbar.frame = toolbarFrame;
  1010.         }
  1011.        
  1012.         if ([toolbarPosition isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
  1013.             toolbarFrame.origin.y = 0;
  1014.             webViewBounds.origin.y += toolbarFrame.size.height;
  1015.             [self setWebViewFrame:webViewBounds];
  1016.         } else {
  1017.             toolbarFrame.origin.y = (webViewBounds.size.height + LOCATIONBAR_HEIGHT);
  1018.         }
  1019.         [self setWebViewFrame:webViewBounds];
  1020.        
  1021.     } else {
  1022.         self.toolbar.hidden = NO;
  1023.        
  1024.         if (locationbarVisible) {
  1025.             // locationBar is on top of toolBar, hide toolBar
  1026.             // put locationBar at the bottom
  1027.            
  1028.             // webView take up whole height less locationBar height
  1029.             CGRect webViewBounds = self.view.bounds;
  1030.             webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
  1031.             [self setWebViewFrame:webViewBounds];
  1032.            
  1033.             // move locationBar down
  1034.             locationbarFrame.origin.y = webViewBounds.size.height;
  1035.             self.addressLabel.frame = locationbarFrame;
  1036.         } else {
  1037.             // no locationBar, expand webView to screen dimensions
  1038.             [self setWebViewFrame:self.view.bounds];
  1039.         }
  1040.     }
  1041. }
  1042.  
  1043. - (void)viewDidLoad
  1044. {
  1045.     [super viewDidLoad];
  1046. }
  1047.  
  1048. - (void)viewDidDisappear:(BOOL)animated
  1049. {
  1050.     [super viewDidDisappear:animated];
  1051.     if (isExiting && (self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserExit)]) {
  1052.         [self.navigationDelegate browserExit];
  1053.         isExiting = FALSE;
  1054.     }
  1055. }
  1056.  
  1057. - (UIStatusBarStyle)preferredStatusBarStyle
  1058. {
  1059.     return UIStatusBarStyleBlackOpaque;
  1060. }
  1061.  
  1062. - (BOOL)prefersStatusBarHidden {
  1063.     return NO;
  1064. }
  1065.  
  1066. - (void)close
  1067. {
  1068.     self.currentURL = nil;
  1069.    
  1070.     __weak UIViewController* weakSelf = self;
  1071.    
  1072.     // Run later to avoid the "took a long time" log message.
  1073.     dispatch_async(dispatch_get_main_queue(), ^{
  1074.         isExiting = TRUE;
  1075.         if ([weakSelf respondsToSelector:@selector(presentingViewController)]) {
  1076.             [[weakSelf presentingViewController] dismissViewControllerAnimated:YES completion:nil];
  1077.         } else {
  1078.             [[weakSelf parentViewController] dismissViewControllerAnimated:YES completion:nil];
  1079.         }
  1080.     });
  1081. }
  1082.  
  1083. - (void)navigateTo:(NSURL*)url
  1084. {
  1085.     if ([url.scheme isEqualToString:@"file"]) {
  1086.         [self.webView loadFileURL:url allowingReadAccessToURL:url];
  1087.     } else {
  1088.         NSURLRequest* request = [NSURLRequest requestWithURL:url];
  1089.         [self.webView loadRequest:request];
  1090.     }
  1091. }
  1092.  
  1093. - (void)goBack:(id)sender
  1094. {
  1095.     [self.webView goBack];
  1096. }
  1097.  
  1098. - (void)goForward:(id)sender
  1099. {
  1100.     [self.webView goForward];
  1101. }
  1102.  
  1103. - (void)viewWillAppear:(BOOL)animated
  1104. {
  1105.     [self rePositionViews];
  1106.    
  1107.     [super viewWillAppear:animated];
  1108. }
  1109.  
  1110. //
  1111. // On iOS 7 the status bar is part of the view's dimensions, therefore it's height has to be taken into account.
  1112. // The height of it could be hardcoded as 20 pixels, but that would assume that the upcoming releases of iOS won't
  1113. // change that value.
  1114. //
  1115. - (float) getStatusBarOffset {
  1116.     return (float) IsAtLeastiOSVersion(@"7.0") ? [[UIApplication sharedApplication] statusBarFrame].size.height : 0.0;
  1117. }
  1118.  
  1119. - (void) rePositionViews {
  1120.     CGRect viewBounds = [self.webView bounds];
  1121.     CGFloat statusBarHeight = [self getStatusBarOffset];
  1122.    
  1123.     // orientation portrait or portraitUpsideDown: status bar is on the top and web view is to be aligned to the bottom of the status bar
  1124.     // orientation landscapeLeft or landscapeRight: status bar height is 0 in but lets account for it in case things ever change in the future
  1125.     viewBounds.origin.y = statusBarHeight;
  1126.    
  1127.     // account for web view height portion that may have been reduced by a previous call to this method
  1128.     viewBounds.size.height = viewBounds.size.height - statusBarHeight + lastReducedStatusBarHeight;
  1129.     lastReducedStatusBarHeight = statusBarHeight;
  1130.    
  1131.     if ((_browserOptions.toolbar) && ([_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop])) {
  1132.         // if we have to display the toolbar on top of the web view, we need to account for its height
  1133.         viewBounds.origin.y += TOOLBAR_HEIGHT;
  1134.         self.toolbar.frame = CGRectMake(self.toolbar.frame.origin.x, statusBarHeight, self.toolbar.frame.size.width, self.toolbar.frame.size.height);
  1135.     }
  1136.    
  1137.     self.webView.frame = viewBounds;
  1138. }
  1139.  
  1140. // Helper function to convert hex color string to UIColor
  1141. // Assumes input like "#00FF00" (#RRGGBB).
  1142. // Taken from https://stackoverflow.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string
  1143. - (UIColor *)colorFromHexString:(NSString *)hexString {
  1144.     unsigned rgbValue = 0;
  1145.     NSScanner *scanner = [NSScanner scannerWithString:hexString];
  1146.     [scanner setScanLocation:1]; // bypass '#' character
  1147.     [scanner scanHexInt:&rgbValue];
  1148.     return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
  1149. }
  1150.  
  1151. #pragma mark WKNavigationDelegate
  1152.  
  1153. - (void)webView:(WKWebView *)theWebView didStartProvisionalNavigation:(WKNavigation *)navigation{
  1154.    
  1155.     // loading url, start spinner, update back/forward
  1156.    
  1157.     self.addressLabel.text = NSLocalizedString(@"Loading...", nil);
  1158.     self.backButton.enabled = theWebView.canGoBack;
  1159.     self.forwardButton.enabled = theWebView.canGoForward;
  1160.    
  1161.     NSLog(_browserOptions.hidespinner ? @"Yes" : @"No");
  1162.     if(!_browserOptions.hidespinner) {
  1163.         [self.spinner startAnimating];
  1164.     }
  1165.    
  1166.     return [self.navigationDelegate didStartProvisionalNavigation:theWebView];
  1167. }
  1168.  
  1169. - (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
  1170. {
  1171.     NSURL *url = navigationAction.request.URL;
  1172.     NSURL *mainDocumentURL = navigationAction.request.mainDocumentURL;
  1173.    
  1174.     BOOL isTopLevelNavigation = [url isEqual:mainDocumentURL];
  1175.    
  1176.     if (isTopLevelNavigation) {
  1177.         self.currentURL = url;
  1178.     }
  1179.    
  1180.     [self.navigationDelegate webView:theWebView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
  1181. }
  1182.  
  1183. - (void)webView:(WKWebView *)theWebView didFinishNavigation:(WKNavigation *)navigation
  1184. {
  1185.     // update url, stop spinner, update back/forward
  1186.    
  1187.     self.addressLabel.text = [self.currentURL absoluteString];
  1188.     self.backButton.enabled = theWebView.canGoBack;
  1189.     self.forwardButton.enabled = theWebView.canGoForward;
  1190.     theWebView.scrollView.contentInset = UIEdgeInsetsZero;
  1191.    
  1192.     [self.spinner stopAnimating];
  1193.    
  1194.     [self.navigationDelegate didFinishNavigation:theWebView];
  1195. }
  1196.    
  1197. - (void)webView:(WKWebView*)theWebView failedNavigation:(NSString*) delegateName withError:(nonnull NSError *)error{
  1198.     // log fail message, stop spinner, update back/forward
  1199.     NSLog(@"webView:%@ - %ld: %@", delegateName, (long)error.code, [error localizedDescription]);
  1200.    
  1201.     self.backButton.enabled = theWebView.canGoBack;
  1202.     self.forwardButton.enabled = theWebView.canGoForward;
  1203.     [self.spinner stopAnimating];
  1204.    
  1205.     self.addressLabel.text = NSLocalizedString(@"Load Error", nil);
  1206.    
  1207.     [self.navigationDelegate webView:theWebView didFailNavigation:error];
  1208. }
  1209.  
  1210. - (void)webView:(WKWebView*)theWebView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
  1211. {
  1212.     [self webView:theWebView failedNavigation:@"didFailNavigation" withError:error];
  1213. }
  1214.    
  1215. - (void)webView:(WKWebView*)theWebView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
  1216. {
  1217.     [self webView:theWebView failedNavigation:@"didFailProvisionalNavigation" withError:error];
  1218. }
  1219.  
  1220. #pragma mark WKScriptMessageHandler delegate
  1221. - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
  1222.     if (![message.name isEqualToString:IAB_BRIDGE_NAME]) {
  1223.         return;
  1224.     }
  1225.     //NSLog(@"Received script message %@", message.body);
  1226.     [self.navigationDelegate userContentController:userContentController didReceiveScriptMessage:message];
  1227. }
  1228.  
  1229. #pragma mark CDVScreenOrientationDelegate
  1230.  
  1231. - (BOOL)shouldAutorotate
  1232. {
  1233.     if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) {
  1234.         return [self.orientationDelegate shouldAutorotate];
  1235.     }
  1236.     return YES;
  1237. }
  1238.  
  1239. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  1240. {
  1241.     if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
  1242.         return [self.orientationDelegate supportedInterfaceOrientations];
  1243.     }
  1244.    
  1245.     return 1 << UIInterfaceOrientationPortrait;
  1246. }
  1247.  
  1248. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  1249. {
  1250.     [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
  1251.     {
  1252.         [self rePositionViews];
  1253.     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
  1254.     {
  1255.  
  1256.     }];
  1257.  
  1258.     [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  1259. }
  1260.  
  1261.  
  1262. @end //CDVWKInAppBrowserViewController
  1263.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement