FlutterWebviewPlugin.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 if ([@"show" isEqualToString:call.method]) {
  43. [self show];
  44. result(nil);
  45. } else if ([@"hide" isEqualToString:call.method]) {
  46. [self hide];
  47. result(nil);
  48. } else {
  49. result(FlutterMethodNotImplemented);
  50. }
  51. }
  52. - (void)initWebview:(FlutterMethodCall*)call {
  53. NSNumber *clearCache = call.arguments[@"clearCache"];
  54. NSNumber *clearCookies = call.arguments[@"clearCookies"];
  55. NSNumber *hidden = call.arguments[@"hidden"];
  56. NSDictionary *rect = call.arguments[@"rect"];
  57. _enableAppScheme = call.arguments[@"enableAppScheme"];
  58. NSString *userAgent = call.arguments[@"userAgent"];
  59. NSNumber *withZoom = call.arguments[@"withZoom"];
  60. if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
  61. [[NSURLCache sharedURLCache] removeAllCachedResponses];
  62. }
  63. if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
  64. [[NSURLSession sharedSession] resetWithCompletionHandler:^{
  65. }];
  66. }
  67. if (userAgent != (id)[NSNull null]) {
  68. [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}];
  69. }
  70. CGRect rc;
  71. if (rect != nil) {
  72. rc = [self parseRect:rect];
  73. } else {
  74. rc = self.viewController.view.bounds;
  75. }
  76. self.webview = [[WKWebView alloc] initWithFrame:rc];
  77. self.webview.navigationDelegate = self;
  78. self.webview.scrollView.delegate = self;
  79. self.webview.hidden = [hidden boolValue];
  80. _enableZoom = [withZoom boolValue];
  81. [self.viewController.view addSubview:self.webview];
  82. [self navigate:call];
  83. }
  84. - (CGRect)parseRect:(NSDictionary *)rect {
  85. return CGRectMake([[rect valueForKey:@"left"] doubleValue],
  86. [[rect valueForKey:@"top"] doubleValue],
  87. [[rect valueForKey:@"width"] doubleValue],
  88. [[rect valueForKey:@"height"] doubleValue]);
  89. }
  90. - (void)navigate:(FlutterMethodCall*)call {
  91. if (self.webview != nil) {
  92. NSString *url = call.arguments[@"url"];
  93. NSNumber *withLocalUrl = call.arguments[@"withLocalUrl"];
  94. if ( [withLocalUrl boolValue]) {
  95. NSURL *htmlUrl = [NSURL fileURLWithPath:url isDirectory:false];
  96. if (@available(iOS 9.0, *)) {
  97. [self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
  98. } else {
  99. @throw @"not available on version earlier than ios 9.0";
  100. }
  101. } else {
  102. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  103. [self.webview loadRequest:request];
  104. }
  105. }
  106. }
  107. - (void)evalJavascript:(FlutterMethodCall*)call
  108. completionHandler:(void (^_Nullable)(NSString * response))completionHandler {
  109. if (self.webview != nil) {
  110. NSString *code = call.arguments[@"code"];
  111. [self.webview evaluateJavaScript:code
  112. completionHandler:^(id _Nullable response, NSError * _Nullable error) {
  113. completionHandler([NSString stringWithFormat:@"%@", response]);
  114. }];
  115. } else {
  116. completionHandler(nil);
  117. }
  118. }
  119. - (void)resize:(FlutterMethodCall*)call {
  120. if (self.webview != nil) {
  121. NSDictionary *rect = call.arguments[@"rect"];
  122. CGRect rc = [self parseRect:rect];
  123. self.webview.frame = rc;
  124. }
  125. }
  126. - (void)closeWebView {
  127. if (self.webview != nil) {
  128. [self.webview stopLoading];
  129. [self.webview removeFromSuperview];
  130. self.webview.navigationDelegate = nil;
  131. self.webview = nil;
  132. // manually trigger onDestroy
  133. [channel invokeMethod:@"onDestroy" arguments:nil];
  134. }
  135. }
  136. - (void)show {
  137. if (self.webview != nil) {
  138. self.webview.hidden = false;
  139. }
  140. }
  141. - (void)hide {
  142. if (self.webview != nil) {
  143. self.webview.hidden = true;
  144. }
  145. }
  146. #pragma mark -- WkWebView Delegate
  147. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
  148. decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  149. id data = @{@"url": navigationAction.request.URL.absoluteString,
  150. @"type": @"shouldStart",
  151. @"navigationType": [NSNumber numberWithInt:navigationAction.navigationType]};
  152. [channel invokeMethod:@"onState" arguments:data];
  153. if (navigationAction.navigationType == WKNavigationTypeBackForward) {
  154. [channel invokeMethod:@"onBackPressed" arguments:nil];
  155. } else {
  156. id data = @{@"url": navigationAction.request.URL.absoluteString};
  157. [channel invokeMethod:@"onUrlChanged" arguments:data];
  158. }
  159. if (_enableAppScheme ||
  160. ([webView.URL.scheme isEqualToString:@"http"] ||
  161. [webView.URL.scheme isEqualToString:@"https"] ||
  162. [webView.URL.scheme isEqualToString:@"about"])) {
  163. decisionHandler(WKNavigationActionPolicyAllow);
  164. } else {
  165. decisionHandler(WKNavigationActionPolicyCancel);
  166. }
  167. }
  168. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
  169. [channel invokeMethod:@"onState" arguments:@{@"type": @"startLoad", @"url": webView.URL.absoluteString}];
  170. }
  171. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  172. [channel invokeMethod:@"onState" arguments:@{@"type": @"finishLoad", @"url": webView.URL.absoluteString}];
  173. }
  174. - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  175. id data = [FlutterError errorWithCode:[NSString stringWithFormat:@"%ld", error.code]
  176. message:error.localizedDescription
  177. details:error.localizedFailureReason];
  178. [channel invokeMethod:@"onError" arguments:data];
  179. }
  180. #pragma mark -- UIScrollViewDelegate
  181. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  182. if (scrollView.pinchGestureRecognizer.isEnabled != _enableZoom) {
  183. scrollView.pinchGestureRecognizer.enabled = _enableZoom;
  184. }
  185. }
  186. @end