FlutterWebviewPlugin.m 5.8 KB

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