CB-13304: expose preference to control WebView navigation
This closes #42
diff --git a/CordovaLib/CordovaLib/Classes/CDVViewController.m b/CordovaLib/CordovaLib/Classes/CDVViewController.m
index 4631b9c..c310403 100644
--- a/CordovaLib/CordovaLib/Classes/CDVViewController.m
+++ b/CordovaLib/CordovaLib/Classes/CDVViewController.m
@@ -88,6 +88,7 @@
WebPreferences* prefs = [self.webView preferences];
[prefs setAutosaves:YES];
+ [self configureWebViewDelegate];
[self configureWebDefaults:prefs];
[self configureLocalStorage:prefs];
[self configureWindowSize];
@@ -165,6 +166,14 @@
self.pluginObjects = [[NSMutableDictionary alloc] initWithCapacity:20];
}
+- (void)configureWebViewDelegate {
+ NSString *allowWebViewNavigation = [self.settings objectForKey:@"AllowWebViewNavigation"];
+ if (allowWebViewNavigation == nil) {
+ allowWebViewNavigation = @"true"; // Default to true for backwards compatibility.
+ }
+ self.webViewDelegate.allowWebViewNavigation = [allowWebViewNavigation boolValue];
+}
+
/**
* Configures the default web preferences
*/
diff --git a/CordovaLib/CordovaLib/Classes/CDVWebViewDelegate.h b/CordovaLib/CordovaLib/Classes/CDVWebViewDelegate.h
index 41f7aeb..9e76690 100644
--- a/CordovaLib/CordovaLib/Classes/CDVWebViewDelegate.h
+++ b/CordovaLib/CordovaLib/Classes/CDVWebViewDelegate.h
@@ -30,6 +30,7 @@
@interface CDVWebViewDelegate : NSObject {
}
+@property (nonatomic) BOOL allowWebViewNavigation;
@property (nonatomic, strong) CDVConsole* console;
@property (nonatomic, strong) CDVBridge* bridge;
@property (nonatomic,
diff --git a/CordovaLib/CordovaLib/Classes/CDVWebViewDelegate.m b/CordovaLib/CordovaLib/Classes/CDVWebViewDelegate.m
index a234e1c..ec99200 100644
--- a/CordovaLib/CordovaLib/Classes/CDVWebViewDelegate.m
+++ b/CordovaLib/CordovaLib/Classes/CDVWebViewDelegate.m
@@ -83,9 +83,15 @@
#pragma mark WebPolicyDelegate
- (void) webView:(WebView*) sender decidePolicyForNavigationAction:(NSDictionary*) actionInformation request:(NSURLRequest*) request frame:(WebFrame*) frame decisionListener:(id <WebPolicyDecisionListener>) listener {
- NSString* url = [[request URL] description];
- NSLog(@"navigating to %@", url);
+ NSURL *url = [request URL];
+ NSLog(@"navigating to %@", url.description);
+ if (self.allowWebViewNavigation || [@"file" isEqualToString:url.scheme]) {
+ // Open in-app if navigation is allowed in the WebView, or if this is a local file request.
[listener use];
+ } else {
+ // Forward external requests to the system otherwise.
+ [[NSWorkspace sharedWorkspace] openURL:url];
+ }
}
#pragma mark WebViewDelegate
diff --git a/bin/templates/scripts/cordova/defaults.xml b/bin/templates/scripts/cordova/defaults.xml
index e28a539..8439080 100644
--- a/bin/templates/scripts/cordova/defaults.xml
+++ b/bin/templates/scripts/cordova/defaults.xml
@@ -23,6 +23,7 @@
<!-- Preferences for OSX -->
<preference name="AllowInlineMediaPlayback" value="true" />
+ <preference name="AllowWebViewNavigation" value="true" />
<preference name="BackupWebStorage" value="cloud" />
<preference name="DisallowOverscroll" value="false" />
<preference name="EnableViewportScale" value="false" />