FlutterWebviewPlugin.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #import "FlutterWebviewPlugin.h"
  2. static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
  3. // UIWebViewDelegate
  4. @interface FlutterWebviewPlugin() <WKNavigationDelegate, UIScrollViewDelegate> {
  5. BOOL _enableAppScheme;
  6. BOOL _enableZoom;
  7. }
  8. @end
  9. @implementation FlutterWebviewPlugin
  10. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  11. channel = [FlutterMethodChannel
  12. methodChannelWithName:CHANNEL_NAME
  13. binaryMessenger:[registrar messenger]];
  14. UIViewController *viewController = (UIViewController *)registrar.messenger;
  15. FlutterWebviewPlugin* instance = [[FlutterWebviewPlugin alloc] initWithViewController:viewController];
  16. [registrar addMethodCallDelegate:instance channel:channel];
  17. }
  18. - (instancetype)initWithViewController:(UIViewController *)viewController {
  19. self = [super init];
  20. if (self) {
  21. self.viewController = viewController;
  22. }
  23. return self;
  24. }
  25. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  26. if ([@"launch" isEqualToString:call.method]) {
  27. if (!self.webview)
  28. [self initWebview:call];
  29. else
  30. [self navigate:call];
  31. result(nil);
  32. } else if ([@"close" isEqualToString:call.method]) {
  33. [self closeWebView];
  34. result(nil);
  35. } else if ([@"eval" isEqualToString:call.method]) {
  36. [self evalJavascript:call completionHandler:^(NSString * response) {
  37. result(response);
  38. }];
  39. } else if ([@"resize" isEqualToString:call.method]) {
  40. [self resize:call];
  41. result(nil);
  42. } else {
  43. result(FlutterMethodNotImplemented);
  44. }
  45. }
  46. - (void)initWebview:(FlutterMethodCall*)call {
  47. NSNumber *clearCache = call.arguments[@"clearCache"];
  48. NSNumber *clearCookies = call.arguments[@"clearCookies"];
  49. NSNumber *hidden = call.arguments[@"hidden"];
  50. NSDictionary *rect = call.arguments[@"rect"];
  51. _enableAppScheme = call.arguments[@"enableAppScheme"];
  52. NSString *userAgent = call.arguments[@"userAgent"];
  53. NSNumber *withZoom = call.arguments[@"withZoom"];
  54. if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
  55. [[NSURLCache sharedURLCache] removeAllCachedResponses];
  56. }
  57. if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
  58. [[NSURLSession sharedSession] resetWithCompletionHandler:^{
  59. }];
  60. }
  61. if (userAgent != (id)[NSNull null]) {
  62. [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}];
  63. }
  64. CGRect rc;
  65. if (rect != nil) {
  66. rc = [self parseRect:rect];
  67. } else {
  68. rc = self.viewController.view.bounds;
  69. }
  70. self.webview = [[WKWebView alloc] initWithFrame:rc];
  71. self.webview.navigationDelegate = self;
  72. [self.viewController.view addSubview:self.webview];
  73. if (withZoom != (id)[NSNull null] && [withZoom boolValue]) {
  74. self.webview.scrollView.delegate = self;
  75. _enableZoom = [withZoom boolValue];
  76. }
  77. if (hidden != (id)[NSNull null] && [hidden boolValue]) {
  78. self.webview.hidden = [hidden boolValue];
  79. }
  80. [self navigate:call];
  81. }
  82. - (CGRect)parseRect:(NSDictionary *)rect {
  83. return CGRectMake([[rect valueForKey:@"left"] doubleValue],
  84. [[rect valueForKey:@"top"] doubleValue],
  85. [[rect valueForKey:@"width"] doubleValue],
  86. [[rect valueForKey:@"height"] doubleValue]);
  87. }
  88. - (void)navigate:(FlutterMethodCall*)call {
  89. if (self.webview != nil) {
  90. NSString *url = call.arguments[@"url"];
  91. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  92. [self.webview loadRequest:request];
  93. }
  94. }
  95. - (void)evalJavascript:(FlutterMethodCall*)call
  96. completionHandler:(void (^_Nullable)(NSString * response))completionHandler {
  97. if (self.webview != nil) {
  98. NSString *code = call.arguments[@"code"];
  99. [self.webview evaluateJavaScript:code
  100. completionHandler:^(id _Nullable response, NSError * _Nullable error) {
  101. completionHandler([NSString stringWithFormat:@"%@", response]);
  102. }];
  103. } else {
  104. completionHandler(nil);
  105. }
  106. }
  107. - (void)resize:(FlutterMethodCall*)call {
  108. if (self.webview != nil) {
  109. NSDictionary *rect = call.arguments[@"rect"];
  110. CGRect rc = [self parseRect:rect];
  111. self.webview.frame = rc;
  112. }
  113. }
  114. - (void)closeWebView {
  115. if (self.webview != nil) {
  116. [self.webview stopLoading];
  117. [self.webview removeFromSuperview];
  118. self.webview.navigationDelegate = nil;
  119. self.webview = nil;
  120. // manually trigger onDestroy
  121. [channel invokeMethod:@"onDestroy" arguments:nil];
  122. }
  123. }
  124. #pragma mark -- WkWebView Delegate
  125. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
  126. decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  127. id data = @{@"url": navigationAction.request.URL.absoluteString,
  128. @"type": @"shouldStart",
  129. @"navigationType": [NSNumber numberWithInt:navigationAction.navigationType]};
  130. [channel invokeMethod:@"onState" arguments:data];
  131. if (navigationAction.navigationType == WKNavigationTypeBackForward) {
  132. [channel invokeMethod:@"onBackPressed" arguments:nil];
  133. } else {
  134. id data = @{@"url": navigationAction.request.URL.absoluteString};
  135. [channel invokeMethod:@"onUrlChanged" arguments:data];
  136. }
  137. if (_enableAppScheme ||
  138. ([webView.URL.scheme isEqualToString:@"http"] ||
  139. [webView.URL.scheme isEqualToString:@"https"] ||
  140. [webView.URL.scheme isEqualToString:@"about"])) {
  141. decisionHandler(WKNavigationActionPolicyAllow);
  142. } else {
  143. decisionHandler(WKNavigationActionPolicyCancel);
  144. }
  145. }
  146. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
  147. [channel invokeMethod:@"onState" arguments:@{@"type": @"startLoad", @"url": webView.URL.absoluteString}];
  148. }
  149. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  150. [channel invokeMethod:@"onState" arguments:@{@"type": @"finishLoad", @"url": webView.URL.absoluteString}];
  151. }
  152. - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  153. id data = [FlutterError errorWithCode:[NSString stringWithFormat:@"%ld", error.code]
  154. message:error.localizedDescription
  155. details:error.localizedFailureReason];
  156. [channel invokeMethod:@"onError" arguments:data];
  157. }
  158. #pragma mark -- UIScrollViewDelegate
  159. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  160. if (scrollView.pinchGestureRecognizer.isEnabled != _enableZoom) {
  161. scrollView.pinchGestureRecognizer.enabled = _enableZoom;
  162. }
  163. }
  164. @end