|
@@ -6,6 +6,7 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
|
|
|
@interface FlutterWebviewPlugin() <WKNavigationDelegate, UIScrollViewDelegate, WKUIDelegate> {
|
|
@interface FlutterWebviewPlugin() <WKNavigationDelegate, UIScrollViewDelegate, WKUIDelegate> {
|
|
|
BOOL _enableAppScheme;
|
|
BOOL _enableAppScheme;
|
|
|
BOOL _enableZoom;
|
|
BOOL _enableZoom;
|
|
|
|
|
+ NSString* _invalidUrlRegex;
|
|
|
}
|
|
}
|
|
|
@end
|
|
@end
|
|
|
|
|
|
|
@@ -83,6 +84,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
|
|
|
NSString *userAgent = call.arguments[@"userAgent"];
|
|
NSString *userAgent = call.arguments[@"userAgent"];
|
|
|
NSNumber *withZoom = call.arguments[@"withZoom"];
|
|
NSNumber *withZoom = call.arguments[@"withZoom"];
|
|
|
NSNumber *scrollBar = call.arguments[@"scrollBar"];
|
|
NSNumber *scrollBar = call.arguments[@"scrollBar"];
|
|
|
|
|
+ NSNumber *withJavascript = call.arguments[@"withJavascript"];
|
|
|
|
|
+ _invalidUrlRegex = call.arguments[@"invalidUrlRegex"];
|
|
|
|
|
|
|
|
if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
|
|
if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
|
|
|
[[NSURLCache sharedURLCache] removeAllCachedResponses];
|
|
[[NSURLCache sharedURLCache] removeAllCachedResponses];
|
|
@@ -112,9 +115,18 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
|
|
|
self.webview.scrollView.showsHorizontalScrollIndicator = [scrollBar boolValue];
|
|
self.webview.scrollView.showsHorizontalScrollIndicator = [scrollBar boolValue];
|
|
|
self.webview.scrollView.showsVerticalScrollIndicator = [scrollBar boolValue];
|
|
self.webview.scrollView.showsVerticalScrollIndicator = [scrollBar boolValue];
|
|
|
|
|
|
|
|
|
|
+ WKPreferences* preferences = [[self.webview configuration] preferences];
|
|
|
|
|
+ if ([withJavascript boolValue]) {
|
|
|
|
|
+ [preferences setJavaScriptEnabled:YES];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ [preferences setJavaScriptEnabled:NO];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
_enableZoom = [withZoom boolValue];
|
|
_enableZoom = [withZoom boolValue];
|
|
|
|
|
|
|
|
- [self.viewController.view addSubview:self.webview];
|
|
|
|
|
|
|
+ UIViewController* presentedViewController = self.viewController.presentedViewController;
|
|
|
|
|
+ UIViewController* currentViewController = presentedViewController != nil ? presentedViewController : self.viewController;
|
|
|
|
|
+ [currentViewController.view addSubview:self.webview];
|
|
|
|
|
|
|
|
[self navigate:call];
|
|
[self navigate:call];
|
|
|
}
|
|
}
|
|
@@ -235,18 +247,37 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
|
|
|
}];
|
|
}];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+- (bool)checkInvalidUrl:(NSURL*)url {
|
|
|
|
|
+ NSString* urlString = url != nil ? [url absoluteString] : nil;
|
|
|
|
|
+ if (_invalidUrlRegex != [NSNull null] && urlString != nil) {
|
|
|
|
|
+ NSError* error = NULL;
|
|
|
|
|
+ NSRegularExpression* regex =
|
|
|
|
|
+ [NSRegularExpression regularExpressionWithPattern:_invalidUrlRegex
|
|
|
|
|
+ options:NSRegularExpressionCaseInsensitive
|
|
|
|
|
+ error:&error];
|
|
|
|
|
+ NSTextCheckingResult* match = [regex firstMatchInString:urlString
|
|
|
|
|
+ options:0
|
|
|
|
|
+ range:NSMakeRange(0, [urlString length])];
|
|
|
|
|
+ return match != nil;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
#pragma mark -- WkWebView Delegate
|
|
#pragma mark -- WkWebView Delegate
|
|
|
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
|
|
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
|
|
|
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
|
|
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
|
|
|
|
|
|
|
|
|
|
+ BOOL isInvalid = [self checkInvalidUrl: navigationAction.request.URL];
|
|
|
|
|
+
|
|
|
id data = @{@"url": navigationAction.request.URL.absoluteString,
|
|
id data = @{@"url": navigationAction.request.URL.absoluteString,
|
|
|
- @"type": @"shouldStart",
|
|
|
|
|
|
|
+ @"type": isInvalid ? @"abortLoad" : @"shouldStart",
|
|
|
@"navigationType": [NSNumber numberWithInt:navigationAction.navigationType]};
|
|
@"navigationType": [NSNumber numberWithInt:navigationAction.navigationType]};
|
|
|
[channel invokeMethod:@"onState" arguments:data];
|
|
[channel invokeMethod:@"onState" arguments:data];
|
|
|
|
|
|
|
|
if (navigationAction.navigationType == WKNavigationTypeBackForward) {
|
|
if (navigationAction.navigationType == WKNavigationTypeBackForward) {
|
|
|
[channel invokeMethod:@"onBackPressed" arguments:nil];
|
|
[channel invokeMethod:@"onBackPressed" arguments:nil];
|
|
|
- } else {
|
|
|
|
|
|
|
+ } else if (!isInvalid) {
|
|
|
id data = @{@"url": navigationAction.request.URL.absoluteString};
|
|
id data = @{@"url": navigationAction.request.URL.absoluteString};
|
|
|
[channel invokeMethod:@"onUrlChanged" arguments:data];
|
|
[channel invokeMethod:@"onUrlChanged" arguments:data];
|
|
|
}
|
|
}
|
|
@@ -255,7 +286,11 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
|
|
|
([webView.URL.scheme isEqualToString:@"http"] ||
|
|
([webView.URL.scheme isEqualToString:@"http"] ||
|
|
|
[webView.URL.scheme isEqualToString:@"https"] ||
|
|
[webView.URL.scheme isEqualToString:@"https"] ||
|
|
|
[webView.URL.scheme isEqualToString:@"about"])) {
|
|
[webView.URL.scheme isEqualToString:@"about"])) {
|
|
|
- decisionHandler(WKNavigationActionPolicyAllow);
|
|
|
|
|
|
|
+ if (isInvalid) {
|
|
|
|
|
+ decisionHandler(WKNavigationActionPolicyCancel);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ decisionHandler(WKNavigationActionPolicyAllow);
|
|
|
|
|
+ }
|
|
|
} else {
|
|
} else {
|
|
|
decisionHandler(WKNavigationActionPolicyCancel);
|
|
decisionHandler(WKNavigationActionPolicyCancel);
|
|
|
}
|
|
}
|