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