FlutterWebviewPlugin.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #import "FlutterWebviewPlugin.h"
  2. static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
  3. @implementation FlutterWebviewPlugin
  4. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  5. channel = [FlutterMethodChannel
  6. methodChannelWithName:CHANNEL_NAME
  7. binaryMessenger:[registrar messenger]];
  8. UIViewController *viewController = (UIViewController *)registrar.messenger;
  9. FlutterWebviewPlugin* instance = [[FlutterWebviewPlugin alloc] initWithViewController:viewController];
  10. [registrar addMethodCallDelegate:instance channel:channel];
  11. }
  12. - (instancetype)initWithViewController:(UIViewController *)viewController {
  13. self = [super init];
  14. if (self) {
  15. self.viewController = viewController;
  16. }
  17. return self;
  18. }
  19. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  20. if ([@"launch" isEqualToString:call.method]) {
  21. [self showWebView:call];
  22. result(nil);
  23. } else if ([@"close" isEqualToString:call.method]) {
  24. [self closeWebView];
  25. result(nil);
  26. } else {
  27. result(FlutterMethodNotImplemented);
  28. }
  29. }
  30. - (void)showWebView:(FlutterMethodCall*)call {
  31. NSString *url = call.arguments[@"url"];
  32. NSNumber *withJavascript = call.arguments[@"withJavascript"];
  33. NSNumber *clearCache = call.arguments[@"clearCache"];
  34. NSNumber *clearCookies = call.arguments[@"clearCookies"];
  35. NSNumber *fullScreen = call.arguments[@"fullScreen"];
  36. self.webviewController = [[WebviewController alloc] initWithUrl:url withJavascript:withJavascript clearCache:clearCache clearCookes:clearCookies];
  37. if ([fullScreen boolValue]) {
  38. [self.viewController presentViewController:self.webviewController animated:YES completion:nil];
  39. } else {
  40. UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:self.webviewController];
  41. [self.viewController presentModalViewController:navigation animated:YES];
  42. }
  43. }
  44. - (void)closeWebView {
  45. [self.webviewController dismissViewControllerAnimated:YES completion:^{
  46. [channel invokeMethod:@"onDestroy" arguments:nil];
  47. }];
  48. }
  49. @end