policy
// in config.xml for cordova-ios-4.0
[self.webViewEngine loadRequest:request];
+#else
+ if ([self.commandDelegate URLIsWhitelisted:url]) {
+ [self.webView loadRequest:request];
+ } else { // this assumes the InAppBrowser can be excepted from the white-list
+ [self openInInAppBrowser:url withOptions:options];
+ }
+#endif
}
- (void)openInSystem:(NSURL*)url
@@ -700,12 +728,13 @@ @implementation CDVWKInAppBrowserViewController
CGFloat lastReducedStatusBarHeight = 0.0;
BOOL isExiting = FALSE;
-- (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary *)settings
+- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent browserOptions: (CDVInAppBrowserOptions*) browserOptions
{
self = [super init];
if (self != nil) {
+ _userAgent = userAgent;
+ _prevUserAgent = prevUserAgent;
_browserOptions = browserOptions;
- _settings = settings;
self.webViewUIDelegate = [[CDVWKInAppBrowserUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
[self.webViewUIDelegate setViewController:self];
@@ -730,15 +759,6 @@ - (void)createViews
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];
@@ -769,9 +789,6 @@ - (void)createViews
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;
@@ -918,11 +935,6 @@ - (void)createViews
[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];
@@ -1090,6 +1102,7 @@ - (BOOL)prefersStatusBarHidden {
- (void)close
{
+ [CDVUserAgentUtil releaseLock:&_userAgentLockToken];
self.currentURL = nil;
__weak UIViewController* weakSelf = self;
@@ -1110,8 +1123,18 @@ - (void)navigateTo:(NSURL*)url
if ([url.scheme isEqualToString:@"file"]) {
[self.webView loadFileURL:url allowingReadAccessToURL:url];
} else {
- NSURLRequest* request = [NSURLRequest requestWithURL:url];
- [self.webView loadRequest:request];
+ NSURLRequest* request = [NSURLRequest requestWithURL:url];
+
+ if (_userAgentLockToken != 0) {
+ [self.webView loadRequest:request];
+ } else {
+ __weak CDVWKInAppBrowserViewController* weakSelf = self;
+ [CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
+ _userAgentLockToken = lockToken;
+ [CDVUserAgentUtil setUserAgent:_userAgent lockToken:lockToken];
+ [weakSelf.webView loadRequest:request];
+ }];
+ }
}
}
@@ -1222,6 +1245,24 @@ - (void)webView:(WKWebView *)theWebView didFinishNavigation:(WKNavigation *)navi
[self.spinner stopAnimating];
+ // Work around a bug where the first time a PDF is opened, all UIWebViews
+ // reload their User-Agent from NSUserDefaults.
+ // This work-around makes the following assumptions:
+ // 1. The app has only a single Cordova Webview. If not, then the app should
+ // take it upon themselves to load a PDF in the background as a part of
+ // their start-up flow.
+ // 2. That the PDF does not require any additional network requests. We change
+ // the user-agent here back to that of the CDVViewController, so requests
+ // from it must pass through its white-list. This *does* break PDFs that
+ // contain links to other remote PDF/websites.
+ // More info at https://issues.apache.org/jira/browse/CB-2225
+ BOOL isPDF = NO;
+ //TODO webview class
+ //BOOL isPDF = [@"true" isEqualToString :[theWebView evaluateJavaScript:@"document.body==null"]];
+ if (isPDF) {
+ [CDVUserAgentUtil setUserAgent:_prevUserAgent lockToken:_userAgentLockToken];
+ }
+
[self.navigationDelegate didFinishNavigation:theWebView];
}
diff --git a/tests/tests.js b/tests/tests.js
index 85d5eba8f..b4d2e68ef 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -182,13 +182,24 @@ exports.defineAutoTests = function () {
});
});
};
- createTests();
+ if (isIos) {
+ createTests('usewkwebview=no');
+ createTests('usewkwebview=yes');
+ } else {
+ createTests();
+ }
};
exports.defineManualTests = function (contentEl, createActionButton) {
var platformOpts = '';
var platform_info = '';
+ if (isIos) {
+ platformOpts = 'usewkwebview=no';
+ platform_info = 'Webview
' +
+ 'Use this button to toggle the Webview implementation.
' +
+ '';
+ }
function doOpen (url, target, params, numExpectedRedirects, useWindowOpen) {
numExpectedRedirects = numExpectedRedirects || 0;
@@ -532,6 +543,20 @@ exports.defineManualTests = function (contentEl, createActionButton) {
var injectjs = isWindows ? basePath + 'inject.js' : 'inject.js';
var injectcss = isWindows ? basePath + 'inject.css' : 'inject.css';
var videohtml = basePath + 'video.html';
+ if (isIos) {
+ createActionButton('Webview=UIWebView', function () {
+ var webviewOption = 'usewkwebview=';
+ var webviewToggle = document.getElementById('webviewToggle');
+ var button = webviewToggle.getElementsByClassName('topcoat-button')[0];
+ if (platformOpts === webviewOption + 'yes') {
+ platformOpts = webviewOption + 'no';
+ button.textContent = 'Webview=UIWebView';
+ } else {
+ platformOpts = webviewOption + 'yes';
+ button.textContent = 'Webview=WKWebView';
+ }
+ }, 'webviewToggle');
+ }
// Local
createActionButton('target=Default', function () {