cordova-plugin-inappbrowser/src/ios/CDVWKInAppBrowser.m
2021-05-11 14:15:05 +08:00

1303 lines
54 KiB
Objective-C

/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import "CDVWKInAppBrowser.h"
#if __has_include("CDVWKProcessPoolFactory.h")
#import "CDVWKProcessPoolFactory.h"
#endif
#import <Cordova/CDVPluginResult.h>
#define kInAppBrowserTargetSelf @"_self"
#define kInAppBrowserTargetSystem @"_system"
#define kInAppBrowserTargetBlank @"_blank"
#define kInAppBrowserToolbarBarPositionBottom @"bottom"
#define kInAppBrowserToolbarBarPositionTop @"top"
#define IAB_BRIDGE_NAME @"cordova_iab"
#define TOOLBAR_HEIGHT 44.0
#define LOCATIONBAR_HEIGHT 21.0
#define FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT))
#define TITLEBAR_HEIGHT 44.0
#pragma mark CDVWKInAppBrowser
@interface CDVWKInAppBrowser () {
NSInteger _previousStatusBarStyle;
}
@end
@implementation CDVWKInAppBrowser
static CDVWKInAppBrowser* instance = nil;
+ (id) getInstance{
return instance;
}
- (void)pluginInitialize
{
instance = self;
_previousStatusBarStyle = -1;
_callbackIdPattern = nil;
_beforeload = @"";
_waitForBeforeload = NO;
}
- (void)onReset
{
[self close:nil];
}
- (void)showStatusbar:(BOOL)show
{
self.statusbar = show;
}
- (void)close:(CDVInvokedUrlCommand*)command
{
if (self.inAppBrowserViewController == nil) {
NSLog(@"IAB.close() called but it was already closed.");
return;
}
// Things are cleaned up in browserExit.
[self.inAppBrowserViewController close];
}
- (BOOL) isSystemUrl:(NSURL*)url
{
if ([[url host] isEqualToString:@"itunes.apple.com"]) {
return YES;
}
return NO;
}
- (void)open:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult;
NSString* url = [command argumentAtIndex:0];
NSString* target = [command argumentAtIndex:1 withDefault:kInAppBrowserTargetSelf];
NSString* options = [command argumentAtIndex:2 withDefault:@"" andClass:[NSString class]];
self.callbackId = command.callbackId;
if (url != nil) {
NSURL* baseUrl = [self.webViewEngine URL];
NSURL* absoluteUrl = [[NSURL URLWithString:url relativeToURL:baseUrl] absoluteURL];
if ([self isSystemUrl:absoluteUrl]) {
target = kInAppBrowserTargetSystem;
}
if ([target isEqualToString:kInAppBrowserTargetSelf]) {
[self openInCordovaWebView:absoluteUrl withOptions:options];
} else if ([target isEqualToString:kInAppBrowserTargetSystem]) {
[self openInSystem:absoluteUrl];
} else { // _blank or anything else
[self openInInAppBrowser:absoluteUrl withOptions:options];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"incorrect number of arguments"];
}
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
{
CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions parseOptions:options];
if(browserOptions.titlebar){
browserOptions.statusbar = NO;
browserOptions.toolbar = NO;
browserOptions.location = NO;
}
self.browserOptions = browserOptions;
WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
if (browserOptions.cleardata) {
NSDate* dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[dataStore removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:dateFrom completionHandler:^{
NSLog(@"Removed all WKWebView data");
self.inAppBrowserViewController.webView.configuration.processPool = [[WKProcessPool alloc] init]; // create new process pool to flush all data
}];
}
if (browserOptions.clearcache) {
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
// Deletes all cookies
WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray* cookies) {
NSHTTPCookie* cookie;
for(cookie in cookies){
[cookieStore deleteCookie:cookie completionHandler:nil];
}
}];
#endif
}else{
// https://stackoverflow.com/a/31803708/777265
// Only deletes domain cookies (not session cookies)
[dataStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {
for (WKWebsiteDataRecord *record in records){
NSSet<NSString*>* dataTypes = record.dataTypes;
if([dataTypes containsObject:WKWebsiteDataTypeCookies]){
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
forDataRecords:@[record]
completionHandler:^{}];
}
}
}];
}
}
if (browserOptions.clearsessioncache) {
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
// Deletes session cookies
WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray* cookies) {
NSHTTPCookie* cookie;
for(cookie in cookies){
if(cookie.sessionOnly){
[cookieStore deleteCookie:cookie completionHandler:nil];
}
}
}];
#endif
}else{
NSLog(@"clearsessioncache not available below iOS 11.0");
}
}
if (self.inAppBrowserViewController == nil) {
self.inAppBrowserViewController = [[CDVWKInAppBrowserViewController alloc] initWithBrowserOptions: browserOptions andSettings:self.commandDelegate.settings andParent: self.viewController];
self.inAppBrowserViewController.navigationDelegate = self;
if ([self.viewController conformsToProtocol:@protocol(CDVScreenOrientationDelegate)]) {
self.inAppBrowserViewController.orientationDelegate = (UIViewController <CDVScreenOrientationDelegate>*)self.viewController;
}
}
[self.inAppBrowserViewController showStatusbar:browserOptions.statusbar];
[self showStatusbar:browserOptions.statusbar];
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar ];
[self.inAppBrowserViewController showTitleBar:browserOptions.titlebar];
[self.inAppBrowserViewController resetWebviewSize:browserOptions.toolbarposition];
if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
int closeButtonIndex = browserOptions.lefttoright ? (browserOptions.hidenavigationbuttons ? 1 : 4) : 0;
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor :closeButtonIndex];
}
// Set Presentation Style
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
if (browserOptions.presentationstyle != nil) {
if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"pagesheet"]) {
presentationStyle = UIModalPresentationPageSheet;
} else if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"formsheet"]) {
presentationStyle = UIModalPresentationFormSheet;
}
}
self.inAppBrowserViewController.modalPresentationStyle = presentationStyle;
// Set Transition Style
UIModalTransitionStyle transitionStyle = UIModalTransitionStyleCoverVertical; // default
if (browserOptions.transitionstyle != nil) {
if ([[browserOptions.transitionstyle lowercaseString] isEqualToString:@"fliphorizontal"]) {
transitionStyle = UIModalTransitionStyleFlipHorizontal;
} else if ([[browserOptions.transitionstyle lowercaseString] isEqualToString:@"crossdissolve"]) {
transitionStyle = UIModalTransitionStyleCrossDissolve;
}
}
self.inAppBrowserViewController.modalTransitionStyle = transitionStyle;
//prevent webView from bouncing
if (browserOptions.disallowoverscroll) {
if ([self.inAppBrowserViewController.webView respondsToSelector:@selector(scrollView)]) {
((UIScrollView*)[self.inAppBrowserViewController.webView scrollView]).bounces = NO;
} else {
for (id subview in self.inAppBrowserViewController.webView.subviews) {
if ([[subview class] isSubclassOfClass:[UIScrollView class]]) {
((UIScrollView*)subview).bounces = NO;
}
}
}
}
// use of beforeload event
if([browserOptions.beforeload isKindOfClass:[NSString class]]){
_beforeload = browserOptions.beforeload;
}else{
_beforeload = @"yes";
}
_waitForBeforeload = ![_beforeload isEqualToString:@""];
[self.inAppBrowserViewController navigateTo:url];
if (!browserOptions.hidden) {
[self show:nil withNoAnimate:browserOptions.hidden];
}
}
- (void)show:(CDVInvokedUrlCommand*)command{
[self show:command withNoAnimate:NO];
}
- (void)show:(CDVInvokedUrlCommand*)command withNoAnimate:(BOOL)noAnimate
{
BOOL initHidden = NO;
if(command == nil && noAnimate == YES){
initHidden = YES;
}
if (self.inAppBrowserViewController == nil) {
NSLog(@"Tried to show IAB after it was closed.");
return;
}
if (_previousStatusBarStyle != -1) {
NSLog(@"Tried to show IAB while already shown");
return;
}
if(!initHidden){
_previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
}
__block CDVInAppBrowserNavigationController* nav = [[CDVInAppBrowserNavigationController alloc]
initWithRootViewController:self.inAppBrowserViewController];
nav.orientationDelegate = self.inAppBrowserViewController;
nav.navigationBarHidden = YES;
nav.modalPresentationStyle = self.inAppBrowserViewController.modalPresentationStyle;
__weak CDVWKInAppBrowser* weakSelf = self;
// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSelf.inAppBrowserViewController != nil) {
/* 开始处理遮挡相机插件问题*/
self.inAppBrowserViewController.view.frame = CGRectMake(0, 0,
self.inAppBrowserViewController.view.frame.size.width,self.inAppBrowserViewController.view.frame.size.height);
[self.viewController.view addSubview:self.inAppBrowserViewController.view];
/* 结束处理遮挡相机插件问题*/
}
});
}
- (void)hide:(CDVInvokedUrlCommand*)command
{
// Set tmpWindow to hidden to make main webview responsive to touch again
// https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
self->tmpWindow.hidden = YES;
self->tmpWindow = nil;
if (self.inAppBrowserViewController == nil) {
NSLog(@"Tried to hide IAB after it was closed.");
return;
}
if (_previousStatusBarStyle == -1) {
NSLog(@"Tried to hide IAB while already hidden");
return;
}
_previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
if (self.inAppBrowserViewController != nil) {
_previousStatusBarStyle = -1;
[self.inAppBrowserViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
});
}
- (void)back:(CDVInvokedUrlCommand *)command
{
if([self.inAppBrowserViewController.webView canGoBack]){
[self.inAppBrowserViewController.webView goBack];
} else {
[self close:(command)];
}
}
- (void)openInCordovaWebView:(NSURL*)url withOptions:(NSString*)options
{
NSURLRequest* request = [NSURLRequest requestWithURL:url];
// the webview engine itself will filter for this according to <allow-navigation> policy
// in config.xml for cordova-ios-4.0
[self.webViewEngine loadRequest:request];
}
- (void)openInSystem:(NSURL*)url
{
if ([[UIApplication sharedApplication] openURL:url] == NO) {
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
[[UIApplication sharedApplication] openURL:url];
}
}
- (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
{
NSString* urlStr = [command argumentAtIndex:0];
if ([_beforeload isEqualToString:@""]) {
NSLog(@"unexpected loadAfterBeforeload called without feature beforeload=get|post");
}
if (self.inAppBrowserViewController == nil) {
NSLog(@"Tried to invoke loadAfterBeforeload on IAB after it was closed.");
return;
}
if (urlStr == nil) {
NSLog(@"loadAfterBeforeload called with nil argument, ignoring.");
return;
}
NSURL* url = [NSURL URLWithString:urlStr];
//_beforeload = @"";
_waitForBeforeload = NO;
[self.inAppBrowserViewController navigateTo:url];
}
// This is a helper method for the inject{Script|Style}{Code|File} API calls, which
// provides a consistent method for injecting JavaScript code into the document.
//
// If a wrapper string is supplied, then the source string will be JSON-encoded (adding
// quotes) and wrapped using string formatting. (The wrapper string should have a single
// '%@' marker).
//
// If no wrapper is supplied, then the source string is executed directly.
- (void)injectDeferredObject:(NSString*)source withWrapper:(NSString*)jsWrapper
{
// Ensure a message handler bridge is created to communicate with the CDVWKInAppBrowserViewController
[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]];
if (jsWrapper != nil) {
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@[source] options:0 error:nil];
NSString* sourceArrayString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
if (sourceArrayString) {
NSString* sourceString = [sourceArrayString substringWithRange:NSMakeRange(1, [sourceArrayString length] - 2)];
NSString* jsToInject = [NSString stringWithFormat:jsWrapper, sourceString];
[self evaluateJavaScript:jsToInject];
}
} else {
[self evaluateJavaScript:source];
}
}
//Synchronus helper for javascript evaluation
- (void)evaluateJavaScript:(NSString *)script {
__block NSString* _script = script;
[self.inAppBrowserViewController.webView evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
NSLog(@"%@", result);
}
} else {
NSLog(@"evaluateJavaScript error : %@ : %@", error.localizedDescription, _script);
}
}];
}
- (void)injectScriptCode:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper = nil;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"_cdvMessageHandler('%@',JSON.stringify([eval(%%@)]));", command.callbackId];
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectScriptFile:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('script'); c.src = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectStyleCode:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('style'); c.innerHTML = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectStyleFile:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
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];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (BOOL)isValidCallbackId:(NSString *)callbackId
{
NSError *err = nil;
// Initialize on first use
if (self.callbackIdPattern == nil) {
self.callbackIdPattern = [NSRegularExpression regularExpressionWithPattern:@"^InAppBrowser[0-9]{1,10}$" options:0 error:&err];
if (err != nil) {
// Couldn't initialize Regex; No is safer than Yes.
return NO;
}
}
if ([self.callbackIdPattern firstMatchInString:callbackId options:0 range:NSMakeRange(0, [callbackId length])]) {
return YES;
}
return NO;
}
/**
* The message handler bridge provided for the InAppBrowser is capable of executing any oustanding callback belonging
* to the InAppBrowser plugin. Care has been taken that other callbacks cannot be triggered, and that no
* other code execution is possible.
*/
- (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL* url = navigationAction.request.URL;
NSURL* mainDocumentURL = navigationAction.request.mainDocumentURL;
BOOL isTopLevelNavigation = [url isEqual:mainDocumentURL];
BOOL shouldStart = YES;
BOOL useBeforeLoad = NO;
NSString* httpMethod = navigationAction.request.HTTPMethod;
NSString* errorMessage = nil;
if([_beforeload isEqualToString:@"post"]){
//TODO handle POST requests by preserving POST data then remove this condition
errorMessage = @"beforeload doesn't yet support POST requests";
}
else if(isTopLevelNavigation && (
[_beforeload isEqualToString:@"yes"]
|| ([_beforeload isEqualToString:@"get"] && [httpMethod isEqualToString:@"GET"])
// TODO comment in when POST requests are handled
// || ([_beforeload isEqualToString:@"post"] && [httpMethod isEqualToString:@"POST"])
)){
useBeforeLoad = YES;
}
// When beforeload, on first URL change, initiate JS callback. Only after the beforeload event, continue.
if (_waitForBeforeload && useBeforeLoad) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"beforeload", @"url":[url absoluteString]}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
if(errorMessage != nil){
NSLog(errorMessage);
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:@{@"type":@"loaderror", @"url":[url absoluteString], @"code": @"-1", @"message": errorMessage}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
//if is an app store link, let the system handle it, otherwise it fails to load it
if ([[ url scheme] isEqualToString:@"itms-appss"] || [[ url scheme] isEqualToString:@"itms-apps"]) {
[theWebView stopLoading];
[self openInSystem:url];
shouldStart = NO;
}
else if ((self.callbackId != nil) && isTopLevelNavigation) {
// Send a loadstart event for each top-level navigation (includes redirects).
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"loadstart", @"url":[url absoluteString]}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
if (useBeforeLoad) {
_waitForBeforeload = YES;
}
if(shouldStart){
// Fix GH-417 & GH-424: Handle non-default target attribute
// Based on https://stackoverflow.com/a/25713070/777265
if (!navigationAction.targetFrame){
[theWebView loadRequest:navigationAction.request];
decisionHandler(WKNavigationActionPolicyCancel);
}else{
decisionHandler(WKNavigationActionPolicyAllow);
}
}else{
decisionHandler(WKNavigationActionPolicyCancel);
}
}
#pragma mark WKScriptMessageHandler delegate
- (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
CDVPluginResult* pluginResult = nil;
if([message.body isKindOfClass:[NSDictionary class]]){
NSDictionary* messageContent = (NSDictionary*) message.body;
NSString* scriptCallbackId = messageContent[@"id"];
if([messageContent objectForKey:@"d"]){
NSString* scriptResult = messageContent[@"d"];
NSError* __autoreleasing error = nil;
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
}
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
}else if(self.callbackId != nil){
// Send a message event
NSString* messageContent = (NSString*) message.body;
NSError* __autoreleasing error = nil;
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[messageContent dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
if (error == nil) {
NSMutableDictionary* dResult = [NSMutableDictionary new];
[dResult setValue:@"message" forKey:@"type"];
[dResult setObject:decodedResult forKey:@"data"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dResult];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}
}
- (void)didStartProvisionalNavigation:(WKWebView*)theWebView
{
NSLog(@"didStartProvisionalNavigation");
// self.inAppBrowserViewController.currentURL = theWebView.URL;
}
- (void)didFinishNavigation:(WKWebView*)theWebView
{
if (self.callbackId != nil) {
NSString* url = [theWebView.URL absoluteString];
if(url == nil){
if(self.inAppBrowserViewController.currentURL != nil){
url = [self.inAppBrowserViewController.currentURL absoluteString];
}else{
url = @"";
}
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"loadstop", @"url":url}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}
- (void)webView:(WKWebView*)theWebView didFailNavigation:(NSError*)error
{
if (self.callbackId != nil) {
NSString* url = [theWebView.URL absoluteString];
if(url == nil){
if(self.inAppBrowserViewController.currentURL != nil){
url = [self.inAppBrowserViewController.currentURL absoluteString];
}else{
url = @"";
}
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:@{@"type":@"loaderror", @"url":url, @"code": [NSNumber numberWithInteger:error.code], @"message": error.localizedDescription}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}
- (void)browserExit
{
if (self.callbackId != nil) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"exit"}];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
self.callbackId = nil;
}
[self.inAppBrowserViewController.configuration.userContentController removeScriptMessageHandlerForName:IAB_BRIDGE_NAME];
self.inAppBrowserViewController.configuration = nil;
[self.inAppBrowserViewController.webView stopLoading];
[self.inAppBrowserViewController.webView removeFromSuperview];
[self.inAppBrowserViewController.webView setUIDelegate:nil];
[self.inAppBrowserViewController.webView setNavigationDelegate:nil];
self.inAppBrowserViewController.webView = nil;
// Set navigationDelegate to nil to ensure no callbacks are received from it.
self.inAppBrowserViewController.navigationDelegate = nil;
self.inAppBrowserViewController = nil;
// Set tmpWindow to hidden to make main webview responsive to touch again
// Based on https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
self->tmpWindow.hidden = YES;
self->tmpWindow = nil;
if (IsAtLeastiOSVersion(@"7.0")) {
if (_previousStatusBarStyle != -1) {
[[UIApplication sharedApplication] setStatusBarStyle:_previousStatusBarStyle];
}
}
_previousStatusBarStyle = -1; // this value was reset before reapplying it. caused statusbar to stay black on ios7
}
@end //CDVWKInAppBrowser
#pragma mark CDVWKInAppBrowserViewController
@implementation CDVWKInAppBrowserViewController
@synthesize currentURL;
BOOL viewRenderedAtLeastOnce = FALSE;
BOOL isExiting = FALSE;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"title"] && object == self.webView)
{
self.titleTitle.text = self.webView.title;
}
}
- (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary *)settings andParent:(UIViewController*) parent
{
self = [super init];
if (self != nil) {
_browserOptions = browserOptions;
_settings = settings;
self.webViewUIDelegate = [[CDVWKInAppBrowserUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
[self.webViewUIDelegate setViewController:self];
self.parent = parent;
[self createViews];
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
return self;
}
-(void)dealloc {
//NSLog(@"dealloc");
}
- (void)createViews
{
// We create the views in code for primarily for ease of upgrades and not requiring an external .xib to be included
CGRect webViewBounds = self.view.bounds;
BOOL toolbarIsAtBottom = ![_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop];
webViewBounds.size.height -= _browserOptions.location ? FOOTER_HEIGHT : TOOLBAR_HEIGHT;
WKUserContentController* userContentController = [[WKUserContentController alloc] init];
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
NSString *userAgent = configuration.applicationNameForUserAgent;
if (
[self settingForKey:@"OverrideUserAgent"] == nil &&
[self settingForKey:@"AppendUserAgent"] != nil
) {
userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [self settingForKey:@"AppendUserAgent"]];
}
configuration.applicationNameForUserAgent = userAgent;
configuration.userContentController = userContentController;
#if __has_include("CDVWKProcessPoolFactory.h")
configuration.processPool = [[CDVWKProcessPoolFactory sharedFactory] sharedProcessPool];
#endif
[configuration.userContentController addScriptMessageHandler:self name:IAB_BRIDGE_NAME];
//WKWebView options
configuration.allowsInlineMediaPlayback = _browserOptions.allowinlinemediaplayback;
if (IsAtLeastiOSVersion(@"10.0")) {
configuration.ignoresViewportScaleLimits = _browserOptions.enableviewportscale;
if(_browserOptions.mediaplaybackrequiresuseraction == YES){
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
}else{
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
}
}else{ // iOS 9
configuration.mediaPlaybackRequiresUserAction = _browserOptions.mediaplaybackrequiresuseraction;
}
self.webView = [[WKWebView alloc] initWithFrame:webViewBounds configuration:configuration];
[self.view addSubview:self.webView];
[self.view sendSubviewToBack:self.webView];
self.webView.navigationDelegate = self;
self.webView.UIDelegate = self.webViewUIDelegate;
self.webView.backgroundColor = [UIColor whiteColor];
if ([self settingForKey:@"OverrideUserAgent"] != nil) {
self.webView.customUserAgent = [self settingForKey:@"OverrideUserAgent"];
}
self.webView.clearsContextBeforeDrawing = YES;
self.webView.clipsToBounds = YES;
self.webView.contentMode = UIViewContentModeScaleToFill;
self.webView.multipleTouchEnabled = YES;
self.webView.opaque = YES;
self.webView.userInteractionEnabled = YES;
self.automaticallyAdjustsScrollViewInsets = YES ;
[self.webView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
self.webView.allowsLinkPreview = NO;
self.webView.allowsBackForwardNavigationGestures = NO;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
if (@available(iOS 11.0, *)) {
[self.webView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
#endif
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.spinner.alpha = 1.000;
self.spinner.autoresizesSubviews = YES;
self.spinner.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin);
self.spinner.clearsContextBeforeDrawing = NO;
self.spinner.clipsToBounds = NO;
self.spinner.contentMode = UIViewContentModeScaleToFill;
self.spinner.frame = CGRectMake(CGRectGetMidX(self.webView.frame), CGRectGetMidY(self.webView.frame), 20.0, 20.0);
self.spinner.hidden = NO;
self.spinner.hidesWhenStopped = YES;
self.spinner.multipleTouchEnabled = NO;
self.spinner.opaque = NO;
self.spinner.userInteractionEnabled = NO;
[self.spinner stopAnimating];
self.closeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
self.closeButton.enabled = YES;
UIBarButtonItem* flexibleSpaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* fixedSpaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpaceButton.width = 20;
float toolbarY = toolbarIsAtBottom ? self.view.bounds.size.height - TOOLBAR_HEIGHT : 0.0;
CGRect toolbarFrame = CGRectMake(0.0, toolbarY, self.view.bounds.size.width, TOOLBAR_HEIGHT);
self.toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
self.toolbar.alpha = 1.000;
self.toolbar.autoresizesSubviews = YES;
self.toolbar.autoresizingMask = toolbarIsAtBottom ? (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin) : UIViewAutoresizingFlexibleWidth;
self.toolbar.barStyle = UIBarStyleBlackOpaque;
self.toolbar.clearsContextBeforeDrawing = NO;
self.toolbar.clipsToBounds = NO;
self.toolbar.contentMode = UIViewContentModeScaleToFill;
self.toolbar.hidden = NO;
self.toolbar.multipleTouchEnabled = NO;
self.toolbar.opaque = NO;
self.toolbar.userInteractionEnabled = YES;
if (_browserOptions.toolbarcolor != nil) { // Set toolbar color if user sets it in options
self.toolbar.barTintColor = [self colorFromHexString:_browserOptions.toolbarcolor];
}
if (!_browserOptions.toolbartranslucent) { // Set toolbar translucent to no if user sets it in options
self.toolbar.translucent = NO;
}
CGFloat labelInset = 5.0;
float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT;
self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelInset, locationBarY, self.view.bounds.size.width - labelInset, LOCATIONBAR_HEIGHT)];
self.addressLabel.adjustsFontSizeToFitWidth = NO;
self.addressLabel.alpha = 1.000;
self.addressLabel.autoresizesSubviews = YES;
self.addressLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
self.addressLabel.backgroundColor = [UIColor clearColor];
self.addressLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
self.addressLabel.clearsContextBeforeDrawing = YES;
self.addressLabel.clipsToBounds = YES;
self.addressLabel.contentMode = UIViewContentModeScaleToFill;
self.addressLabel.enabled = YES;
self.addressLabel.hidden = NO;
self.addressLabel.lineBreakMode = NSLineBreakByTruncatingTail;
if ([self.addressLabel respondsToSelector:NSSelectorFromString(@"setMinimumScaleFactor:")]) {
[self.addressLabel setValue:@(10.0/[UIFont labelFontSize]) forKey:@"minimumScaleFactor"];
} else if ([self.addressLabel respondsToSelector:NSSelectorFromString(@"setMinimumFontSize:")]) {
[self.addressLabel setValue:@(10.0) forKey:@"minimumFontSize"];
}
self.addressLabel.multipleTouchEnabled = NO;
self.addressLabel.numberOfLines = 1;
self.addressLabel.opaque = NO;
self.addressLabel.shadowOffset = CGSizeMake(0.0, -1.0);
self.addressLabel.text = NSLocalizedString(@"Loading...", nil);
self.addressLabel.textAlignment = NSTextAlignmentLeft;
self.addressLabel.textColor = [UIColor colorWithWhite:1.000 alpha:1.000];
self.addressLabel.userInteractionEnabled = NO;
NSString* frontArrowString = NSLocalizedString(@"", nil); // create arrow from Unicode char
self.forwardButton = [[UIBarButtonItem alloc] initWithTitle:frontArrowString style:UIBarButtonItemStylePlain target:self action:@selector(goForward:)];
self.forwardButton.enabled = YES;
self.forwardButton.imageInsets = UIEdgeInsetsZero;
if (_browserOptions.navigationbuttoncolor != nil) { // Set button color if user sets it in options
self.forwardButton.tintColor = [self colorFromHexString:_browserOptions.navigationbuttoncolor];
}
NSString* backArrowString = NSLocalizedString(@"", nil); // create arrow from Unicode char
self.backButton = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
self.backButton.enabled = YES;
self.backButton.imageInsets = UIEdgeInsetsZero;
if (_browserOptions.navigationbuttoncolor != nil) { // Set button color if user sets it in options
self.backButton.tintColor = [self colorFromHexString:_browserOptions.navigationbuttoncolor];
}
// Filter out Navigation Buttons if user requests so
if (_browserOptions.hidenavigationbuttons) {
if (_browserOptions.lefttoright) {
[self.toolbar setItems:@[flexibleSpaceButton, self.closeButton]];
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
}
} else if (_browserOptions.lefttoright) {
[self.toolbar setItems:@[self.backButton, fixedSpaceButton, self.forwardButton, flexibleSpaceButton, self.closeButton]];
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
}
// 状态栏和标题栏的通用配置
float offsetY=[UIApplication sharedApplication].statusBarFrame.size.height;
UIColor *backgroundColor = nil;
if(_browserOptions.background == nil) {
backgroundColor = [UIColor grayColor];
}else{
backgroundColor = [self colorFromHexString:_browserOptions.background];
}
UIColor *fontColor = nil;
if(_browserOptions.color == nil) {
fontColor = [UIColor whiteColor];
}else{
fontColor = [self colorFromHexString:_browserOptions.color];
}
// 开始添加状态栏
self.statusbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, offsetY)];
self.statusbar.backgroundColor = backgroundColor;
self.statusbar.hidden = YES;
// 结束添加状态栏
// 开始添加标题栏
self.titlebar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, TITLEBAR_HEIGHT + offsetY)]; //self.navigationController.navigationBar;
self.titlebar.backgroundColor = backgroundColor;
self.titlebar.tintColor =fontColor;
// 返回按钮
self.titleBackButton =[[UIButton alloc] initWithFrame:CGRectMake(self.view.safeAreaInsets.left, offsetY, TITLEBAR_HEIGHT, TITLEBAR_HEIGHT)];
[self.titleBackButton setTitle:@"" forState:UIControlStateNormal];
[self.titleBackButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
if(_browserOptions.backbutton){
[self.titlebar addSubview:self.titleBackButton];
}
self.titleCloseButton =[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - TITLEBAR_HEIGHT - self.view.safeAreaInsets.right, offsetY, TITLEBAR_HEIGHT, TITLEBAR_HEIGHT)];
[self.titleCloseButton setTitle:@"" forState:UIControlStateNormal];
[self.titleCloseButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
[self.titlebar addSubview:self.titleCloseButton];
self.titleTitle = [[UILabel alloc] initWithFrame:CGRectMake(self.titleBackButton.frame.origin.x + self.titleBackButton.frame.size.width,offsetY,
self.view.frame.size.width - TITLEBAR_HEIGHT - self.view.safeAreaInsets.right - self.titleBackButton.frame.size.width - self.titleBackButton.frame.origin.x, TITLEBAR_HEIGHT)];
[self.titleTitle setText:@"加载中..."];
self.titleTitle.textAlignment = NSTextAlignmentCenter;
self.titleTitle.numberOfLines = 1;
[self.titleTitle setTextColor:fontColor];
[self.titlebar addSubview:self.titleTitle];
// 结束添加标题栏
self.view.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.statusbar];
[self.view addSubview:self.toolbar];
[self.view addSubview:self.addressLabel];
[self.view addSubview:self.titlebar];
[self.view addSubview:self.spinner];
}
- (id)settingForKey:(NSString*)key
{
return [_settings objectForKey:[key lowercaseString]];
}
- (void) setWebViewFrame : (CGRect) frame {
NSLog(@"Setting the WebView's frame to %@", NSStringFromCGRect(frame));
[self.webView setFrame:frame];
}
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex
{
// the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
// 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)
self.closeButton = nil;
// Initialize with title if title is set, otherwise the title will be 'Done' localized
self.closeButton = title != nil ? [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:@selector(close)] : [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
self.closeButton.enabled = YES;
// If color on closebutton is requested then initialize with that that color, otherwise use initialize with default
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];
NSMutableArray* items = [self.toolbar.items mutableCopy];
[items replaceObjectAtIndex:buttonIndex withObject:self.closeButton];
[self.toolbar setItems:items];
}
- (void)showStatusbar:(BOOL)show
{
self.hasStatusbar = show;
}
- (void)showLocationBar:(BOOL)show
{
self.hasLocationbar = show;
}
- (void)showToolBar:(BOOL)show
{
self.hasToolbar = show;
}
- (void)showTitleBar:(BOOL)show
{
self.hasTitlebar = show;
}
-(void) resetWebviewSize: (NSString *) toolbarPosition{
float offsetY=[UIApplication sharedApplication].statusBarFrame.size.height;
CGRect webViewBounds = self.view.bounds;
CGRect toolbarFrame = self.toolbar.frame;
CGRect locationbarFrame = self.addressLabel.frame;
if(self.hasTitlebar){
self.hasStatusbar = NO;
self.hasToolbar = NO;
self.hasLocationbar = NO;
}
if(self.hasStatusbar){
self.statusbar.hidden = NO;
webViewBounds.size.height -= offsetY;
webViewBounds.origin.y +=offsetY;
} else {
self.statusbar.hidden = YES;
}
if(self.hasLocationbar){
self.addressLabel.hidden = NO;
webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
self.addressLabel.frame = locationbarFrame;
locationbarFrame.origin.y = webViewBounds.origin.y + webViewBounds.size.height ;
} else {
self.addressLabel.hidden = YES;
}
if(self.hasToolbar){
self.toolbar.hidden = NO;
webViewBounds.size.height -= TOOLBAR_HEIGHT;
self.toolbar.frame = toolbarFrame;
if ([toolbarPosition isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
toolbarFrame.origin.y = webViewBounds.origin.y;
if(self.hasLocationbar){
locationbarFrame.origin.y = toolbarFrame.origin.y + toolbarFrame.size.height ;
webViewBounds.origin.y += locationbarFrame.size.height;
}
webViewBounds.origin.y += toolbarFrame.size.height;
[self setWebViewFrame:webViewBounds];
} else {
toolbarFrame.origin.y = webViewBounds.size.height + webViewBounds.origin.y;
if(self.hasLocationbar){
locationbarFrame.origin.y = webViewBounds.origin.y + webViewBounds.size.height ;
toolbarFrame.origin.y += locationbarFrame.size.height;
}
}
} else {
self.toolbar.hidden = YES;
}
if(self.hasTitlebar){
self.titlebar.hidden = NO;
webViewBounds.size.height -= offsetY + TITLEBAR_HEIGHT;
webViewBounds.origin.y =offsetY + TITLEBAR_HEIGHT;
} else {
self.titlebar.hidden = YES;
}
if(self.hasLocationbar){
}
[self setWebViewFrame:webViewBounds];
}
-(void) viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
NSLog(@"viewDidLayoutSubviews");
[self rePositionViews];
}
- (void)viewDidLoad
{
viewRenderedAtLeastOnce = FALSE;
[super viewDidLoad];
NSLog(@"viewDidLoad");
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if (isExiting && (self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserExit)]) {
[self.navigationDelegate browserExit];
isExiting = FALSE;
}
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
/// 判断当前是否使用暗黑外观
- (BOOL)inDarkAppearance{
BOOL res = NO;
if (@available(iOS 13.0, *)) {
switch (UITraitCollection.currentTraitCollection.userInterfaceStyle) {
case UIUserInterfaceStyleDark:
NSLog(@"深色模式");
res = YES;
break;
case UIUserInterfaceStyleLight:
NSLog(@"浅色模式");
break;
case UIUserInterfaceStyleUnspecified:
NSLog(@"未指定");
break;
}
}
return res;
}
- (BOOL)prefersStatusBarHidden {
return !self.statusbar;
}
- (void)close
{
self.currentURL = nil;
[self.webView removeObserver:self forKeyPath:@"title" context: NULL];
__weak UIViewController* weakSelf = self;
__weak UIViewController* weakParentSelf = self.parent;
// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
UIView *lastView;
for(UIView *subview in [weakParentSelf.view subviews]) {
lastView = subview;
}
[lastView removeFromSuperview];
isExiting = TRUE;
if ([weakSelf respondsToSelector:@selector(presentingViewController)]) {
[[weakSelf presentingViewController] dismissViewControllerAnimated:YES completion:nil];
} else {
[[weakSelf parentViewController] dismissViewControllerAnimated:YES completion:nil];
}
});
}
- (void)navigateTo:(NSURL*)url
{
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
- (void)goBack:(id)sender
{
if(self.webView.canGoBack){
[self.webView goBack];
} else {
[self close];
}
}
- (void)goForward:(id)sender
{
[self.webView goForward];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
//
// On iOS 7 the status bar is part of the view's dimensions, therefore it's height has to be taken into account.
// The height of it could be hardcoded as 20 pixels, but that would assume that the upcoming releases of iOS won't
// change that value.
//
- (float) getStatusBarOffset {
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
float statusBarOffset = IsAtLeastiOSVersion(@"7.0") ? MIN(statusBarFrame.size.width, statusBarFrame.size.height) : 0.0;
return statusBarOffset;
}
- (void) rePositionViews {
if ([_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
[self.webView setFrame:CGRectMake(self.webView.frame.origin.x, TOOLBAR_HEIGHT, self.webView.frame.size.width, self.webView.frame.size.height)];
[self.toolbar setFrame:CGRectMake(self.toolbar.frame.origin.x, [self getStatusBarOffset], self.toolbar.frame.size.width, self.toolbar.frame.size.height)];
}
}
// Helper function to convert hex color string to UIColor
// Assumes input like "#00FF00" (#RRGGBB).
- (UIColor *)colorFromHexString:(NSString *)hexString {
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}
#pragma mark WKNavigationDelegate
- (void)webView:(WKWebView *)theWebView didStartProvisionalNavigation:(WKNavigation *)navigation{
// loading url, start spinner, update back/forward
self.addressLabel.text = NSLocalizedString(@"Loading...", nil);
[self.titleTitle setText:@"加载中..."];
self.backButton.enabled = theWebView.canGoBack;
self.forwardButton.enabled = theWebView.canGoForward;
NSLog(_browserOptions.hidespinner ? @"Yes" : @"No");
if(!_browserOptions.hidespinner) {
[self.spinner startAnimating];
}
return [self.navigationDelegate didStartProvisionalNavigation:theWebView];
}
- (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSURL *url = navigationAction.request.URL;
NSURL *mainDocumentURL = navigationAction.request.mainDocumentURL;
BOOL isTopLevelNavigation = [url isEqual:mainDocumentURL];
if (isTopLevelNavigation) {
self.currentURL = url;
}
[self.navigationDelegate webView:theWebView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
}
- (void)webView:(WKWebView *)theWebView didFinishNavigation:(WKNavigation *)navigation
{
// update url, stop spinner, update back/forward
self.addressLabel.text = [self.currentURL absoluteString];
self.backButton.enabled = theWebView.canGoBack;
self.forwardButton.enabled = theWebView.canGoForward;
theWebView.scrollView.contentInset = UIEdgeInsetsZero;
[self.titleTitle setText:theWebView.title];
// self.titlebar.hidden = NO;
[self.spinner stopAnimating];
[self.navigationDelegate didFinishNavigation:theWebView];
}
- (void)webView:(WKWebView*)theWebView failedNavigation:(NSString*) delegateName withError:(nonnull NSError *)error{
// log fail message, stop spinner, update back/forward
NSLog(@"webView:%@ - %ld: %@", delegateName, (long)error.code, [error localizedDescription]);
self.backButton.enabled = theWebView.canGoBack;
self.forwardButton.enabled = theWebView.canGoForward;
[self.spinner stopAnimating];
[self.titleTitle setText: @"加载出错"];
if(!self.hasToolbar){
self.titlebar.hidden = NO;
self.titleBackButton.hidden = YES;
}
self.addressLabel.text = NSLocalizedString(@"Load Error", nil);
[self.navigationDelegate webView:theWebView didFailNavigation:error];
}
- (void)webView:(WKWebView*)theWebView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
{
[self webView:theWebView failedNavigation:@"didFailNavigation" withError:error];
}
- (void)webView:(WKWebView*)theWebView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
{
[self webView:theWebView failedNavigation:@"didFailProvisionalNavigation" withError:error];
}
#pragma mark WKScriptMessageHandler delegate
- (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
if (![message.name isEqualToString:IAB_BRIDGE_NAME]) {
return;
}
//NSLog(@"Received script message %@", message.body);
[self.navigationDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
#pragma mark CDVScreenOrientationDelegate
- (BOOL)shouldAutorotate
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) {
return [self.orientationDelegate shouldAutorotate];
}
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.orientationDelegate supportedInterfaceOrientations];
}
return 1 << UIInterfaceOrientationPortrait;
}
@end //CDVWKInAppBrowserViewController