FlutterWebviewPlugin.m 7.3 KB

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