- 
                Notifications
    
You must be signed in to change notification settings  - Fork 940
 
Using WKWebView instead of UIWebView #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| #import <Flutter/Flutter.h> | ||
| #import <WebKit/WebKit.h> | ||
| 
     | 
||
| static FlutterMethodChannel *channel; | ||
| 
     | 
||
| @interface FlutterWebviewPlugin : NSObject<FlutterPlugin> | ||
| @property (nonatomic, retain) UIViewController *viewController; | ||
| @property (nonatomic, retain) UIWebView *webview; | ||
| @property (nonatomic, retain) WKWebView *webview; | ||
| @end | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -3,8 +3,9 @@ | |
| static NSString *const CHANNEL_NAME = @"flutter_webview_plugin"; | ||
| 
     | 
||
| // UIWebViewDelegate | ||
| @interface FlutterWebviewPlugin() <UIWebViewDelegate> { | ||
| @interface FlutterWebviewPlugin() <WKNavigationDelegate, UIScrollViewDelegate> { | ||
| BOOL _enableAppScheme; | ||
| BOOL _enableZoom; | ||
| } | ||
| @end | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -39,7 +40,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { | |
| [self closeWebView]; | ||
| result(nil); | ||
| } else if ([@"eval" isEqualToString:call.method]) { | ||
| result([self evalJavascript:call]); | ||
| [self evalJavascript:call completionHandler:^(NSString * response) { | ||
| result(response); | ||
| }]; | ||
| } else if ([@"resize" isEqualToString:call.method]) { | ||
| [self resize:call]; | ||
| result(nil); | ||
| 
        
          
        
         | 
    @@ -49,7 +52,6 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { | |
| } | ||
| 
     | 
||
| - (void)initWebview:(FlutterMethodCall*)call { | ||
| // NSNumber *withJavascript = call.arguments[@"withJavascript"]; | ||
| NSNumber *clearCache = call.arguments[@"clearCache"]; | ||
| NSNumber *clearCookies = call.arguments[@"clearCookies"]; | ||
| NSNumber *hidden = call.arguments[@"hidden"]; | ||
| 
        
          
        
         | 
    @@ -58,7 +60,6 @@ - (void)initWebview:(FlutterMethodCall*)call { | |
| NSString *userAgent = call.arguments[@"userAgent"]; | ||
| NSNumber *withZoom = call.arguments[@"withZoom"]; | ||
| 
     | 
||
| // | ||
| if (clearCache != (id)[NSNull null] && [clearCache boolValue]) { | ||
| [[NSURLCache sharedURLCache] removeAllCachedResponses]; | ||
| } | ||
| 
        
          
        
         | 
    @@ -79,17 +80,18 @@ - (void)initWebview:(FlutterMethodCall*)call { | |
| rc = self.viewController.view.bounds; | ||
| } | ||
| 
     | 
||
| self.webview = [[UIWebView alloc] initWithFrame:rc]; | ||
| self.webview.delegate = self; | ||
| 
     | 
||
| self.webview = [[WKWebView alloc] initWithFrame:rc]; | ||
| self.webview.navigationDelegate = self; | ||
| [self.viewController.view addSubview:self.webview]; | ||
| 
     | 
||
| if (withZoom != (id)[NSNull null] && [withZoom boolValue]) { | ||
| self.webview.scalesPageToFit = YES; | ||
| self.webview.scrollView.delegate = self; | ||
| _enableZoom = [withZoom boolValue]; | ||
| } | ||
| 
     | 
||
| if (hidden != (id)[NSNull null] && [hidden boolValue]) { | ||
                
       | 
||
| self.webview.hidden = YES; | ||
| self.webview.hidden = [hidden boolValue]; | ||
| } | ||
| [self.viewController.view addSubview:self.webview]; | ||
| 
     | 
||
| [self navigate:call]; | ||
| } | ||
| 
        
          
        
         | 
    @@ -102,73 +104,92 @@ - (CGRect)parseRect:(NSDictionary *)rect { | |
| } | ||
| 
     | 
||
| - (void)navigate:(FlutterMethodCall*)call { | ||
| NSString *url = call.arguments[@"url"]; | ||
| 
     | 
||
| NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; | ||
| [self.webview loadRequest:request]; | ||
| if (self.webview != nil) { | ||
| NSString *url = call.arguments[@"url"]; | ||
| NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; | ||
| [self.webview loadRequest:request]; | ||
| } | ||
| } | ||
| 
     | 
||
| - (NSString *)evalJavascript:(FlutterMethodCall*)call { | ||
| NSString *code = call.arguments[@"code"]; | ||
| 
     | 
||
| NSString *result = [self.webview stringByEvaluatingJavaScriptFromString:code]; | ||
| return result; | ||
| - (void)evalJavascript:(FlutterMethodCall*)call | ||
| completionHandler:(void (^_Nullable)(NSString * response))completionHandler { | ||
| if (self.webview != nil) { | ||
| NSString *code = call.arguments[@"code"]; | ||
| [self.webview evaluateJavaScript:code | ||
| completionHandler:^(id _Nullable response, NSError * _Nullable error) { | ||
| completionHandler([NSString stringWithFormat:@"%@", response]); | ||
| }]; | ||
| } else { | ||
| completionHandler(nil); | ||
| } | ||
| } | ||
| 
     | 
||
| - (void)resize:(FlutterMethodCall*)call { | ||
| NSDictionary *rect = call.arguments[@"rect"]; | ||
| CGRect rc = [self parseRect:rect]; | ||
| self.webview.frame = rc; | ||
| if (self.webview != nil) { | ||
| NSDictionary *rect = call.arguments[@"rect"]; | ||
| CGRect rc = [self parseRect:rect]; | ||
| self.webview.frame = rc; | ||
| } | ||
| } | ||
| 
     | 
||
| - (void)closeWebView { | ||
| [self.webview stopLoading]; | ||
| [self.webview removeFromSuperview]; | ||
| self.webview.delegate = nil; | ||
| self.webview = nil; | ||
| 
     | 
||
| // manually trigger onDestroy | ||
| [channel invokeMethod:@"onDestroy" arguments:nil]; | ||
| if (self.webview != nil) { | ||
| [self.webview stopLoading]; | ||
| [self.webview removeFromSuperview]; | ||
| self.webview.navigationDelegate = nil; | ||
| self.webview = nil; | ||
| 
     | 
||
| // manually trigger onDestroy | ||
| [channel invokeMethod:@"onDestroy" arguments:nil]; | ||
| } | ||
| } | ||
| 
     | 
||
| #pragma mark -- WkWebView Delegate | ||
| - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction | ||
| decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { | ||
| 
     | 
||
| #pragma mark -- WebView Delegate | ||
| - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { | ||
| id data = @{@"url": request.URL.absoluteString, | ||
| id data = @{@"url": navigationAction.request.URL.absoluteString, | ||
| @"type": @"shouldStart", | ||
| @"navigationType": [NSNumber numberWithInt:navigationType]}; | ||
| @"navigationType": [NSNumber numberWithInt:navigationAction.navigationType]}; | ||
| [channel invokeMethod:@"onState" arguments:data]; | ||
| if (navigationType == UIWebViewNavigationTypeBackForward) | ||
| 
     | 
||
| if (navigationAction.navigationType == WKNavigationTypeBackForward) { | ||
| [channel invokeMethod:@"onBackPressed" arguments:nil]; | ||
| else { | ||
| id data = @{@"url": request.URL.absoluteString}; | ||
| } else { | ||
| id data = @{@"url": navigationAction.request.URL.absoluteString}; | ||
| [channel invokeMethod:@"onUrlChanged" arguments:data]; | ||
| } | ||
| 
     | 
||
| if (_enableAppScheme) | ||
| return YES; | ||
| 
     | 
||
| // disable some scheme | ||
| return [request.URL.scheme isEqualToString:@"http"] || | ||
| [request.URL.scheme isEqualToString:@"https"] || | ||
| [request.URL.scheme isEqualToString:@"about"]; | ||
| if (_enableAppScheme || | ||
| ([webView.URL.scheme isEqualToString:@"http"] || | ||
| [webView.URL.scheme isEqualToString:@"https"] || | ||
| [webView.URL.scheme isEqualToString:@"about"])) { | ||
| decisionHandler(WKNavigationActionPolicyAllow); | ||
| } else { | ||
| decisionHandler(WKNavigationActionPolicyCancel); | ||
| } | ||
| } | ||
| 
     | 
||
| -(void)webViewDidStartLoad:(UIWebView *)webView { | ||
| [channel invokeMethod:@"onState" arguments:@{@"type": @"startLoad", @"url": webView.request.URL.absoluteString}]; | ||
| - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation { | ||
| [channel invokeMethod:@"onState" arguments:@{@"type": @"startLoad", @"url": webView.URL.absoluteString}]; | ||
| } | ||
| 
     | 
||
| - (void)webViewDidFinishLoad:(UIWebView *)webView { | ||
| [channel invokeMethod:@"onState" arguments:@{@"type": @"finishLoad", @"url": webView.request.URL.absoluteString}]; | ||
| - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { | ||
| [channel invokeMethod:@"onState" arguments:@{@"type": @"finishLoad", @"url": webView.URL.absoluteString}]; | ||
| } | ||
| 
     | 
||
| - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { | ||
| - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error { | ||
| id data = [FlutterError errorWithCode:[NSString stringWithFormat:@"%ld", error.code] | ||
| message:error.localizedDescription | ||
| details:error.localizedFailureReason]; | ||
| [channel invokeMethod:@"onError" arguments:data]; | ||
| } | ||
| 
     | 
||
| #pragma mark -- WkWebView Delegate | ||
| #pragma mark -- UIScrollViewDelegate | ||
| - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { | ||
| if (scrollView.pinchGestureRecognizer.isEnabled != _enableZoom) { | ||
| scrollView.pinchGestureRecognizer.enabled = _enableZoom; | ||
| } | ||
| } | ||
| 
     | 
||
| @end | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same thing here, can we remove the condition ?