Flutter官方的webview_flutter ios中使用的wkwebview來實(shí)現(xiàn)的率触,webview加載本地html 無法加載js文件, 網(wǎng)上找了很多方法都不行踱葛、后來發(fā)現(xiàn)只要在FlutterWebview initWithFrame方法中加上這一句話就好了饭尝,另外注意需要在當(dāng)前app的沙盒中俘陷。
[configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
完整的代碼
- (instancetype)initWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger {
if (self = [super init]) {
_viewId = viewId;
NSString* channelName = [NSString stringWithFormat:@"plugins.flutter.io/webview_%lld", viewId];
_channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:messenger];
_javaScriptChannelNames = [[NSMutableSet alloc] init];
WKUserContentController* userContentController = [[WKUserContentController alloc] init];
if ([args[@"javascriptChannelNames"] isKindOfClass:[NSArray class]]) {
NSArray* javaScriptChannelNames = args[@"javascriptChannelNames"];
[_javaScriptChannelNames addObjectsFromArray:javaScriptChannelNames];
[self registerJavaScriptChannels:_javaScriptChannelNames controller:userContentController];
}
NSDictionary<NSString*, id>* settings = args[@"settings"];
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
// WKUserScript *script = [[WKUserScript alloc] initWithSource:[self jsString] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
// [configuration.userContentController addUserScript:script];
[configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
configuration.userContentController = userContentController;
[self updateAutoMediaPlaybackPolicy:args[@"autoMediaPlaybackPolicy"]
inConfiguration:configuration];
_webView = [[FLTWKWebView alloc] initWithFrame:frame configuration:configuration];
_navigationDelegate = [[FLTWKNavigationDelegate alloc] initWithChannel:_channel];
_webView.UIDelegate = self;
_webView.navigationDelegate = _navigationDelegate;
__weak __typeof__(self) weakSelf = self;
[_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
[weakSelf onMethodCall:call result:result];
}];
if (@available(iOS 11.0, *)) {
_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
if (@available(iOS 13.0, *)) {
_webView.scrollView.automaticallyAdjustsScrollIndicatorInsets = NO;
}
}
[self applySettings:settings];
// TODO(amirh): return an error if apply settings failed once it's possible to do so.
// https://github.com/flutter/flutter/issues/36228
NSString* initialUrl = args[@"initialUrl"];
if ([initialUrl isKindOfClass:[NSString class]]) {
[self loadUrl:initialUrl];
}
}
return self;
}
android的話增加這句話,容許加載本地文件url
webView.getSettings().setAllowFileAccessFromFileURLs(true);
以下方法是幾種不行的方法嘗試愿待;也是作下記錄浩螺。
//- (bool)loadUrl:(NSString*)url withHeaders:(NSDictionary<NSString*, NSString*>*)headers {
// NSURL* nsUrl = [NSURL URLWithString:url];
// if (!nsUrl) {
// return false;
// }
// NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:nsUrl];
// [request setAllHTTPHeaderFields:headers];
////判斷是url,然后使用不同的加載方式
// if([url hasPrefix:@"http"]) {
// [_webView loadRequest:request];
// NSLog(@"zb....%@",@"loadRequest");
// }else{
// [_webView loadFileURL:nsUrl allowingReadAccessToURL:[nsUrl URLByDeletingLastPathComponent]];
//
// // NSURL *pathURL = [NSURL fileURLWithPath:url];
//// NSURL* nsUrl = [NSURL URLWithString:@"file:///Users/zhoubo5/jdflutter_webview/dart_2_js_web_example/build/web"];
////
//// NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
////
//// NSString *path = [document stringByAppendingString:@"/web/index.html"];
//// NSURL *pathURL = [NSURL fileURLWithPath:path];
//// NSString *path1 = [document stringByAppendingString:@"/web"];
//// NSURL *pathURL2 = [NSURL fileURLWithPath:path1];
//
//// NSURL *accessURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
//// [self.webview loadFileURL:self.url allowingReadAccessToURL:accessURL];
//
//// [_webView loadFileURL:pathURL allowingReadAccessToURL:pathURL2];
//
//
//
//// //項(xiàng)目中的文件夾路徑
//// NSString *directoryPath = [NSFileManager appSourceName:@"FeedbackH5" andType:@""];
////
//// //tmp緩存文件夾路徑
//// NSString *tmpPath = [KFileManger tmpPath];
////
//// //新文件夾名字
//// NSString *wwwDir =@"www";
////
//// //tmp文件夾下創(chuàng)建www文件夾
//// [KFileManger createDirWithPath:tmpPath andDirectoryName: wwwDir];
////
//// //tmp中的www文件夾中的路徑
//// NSString *tmpWWW = [tmpPath stringByAppendingString: wwwDir];
////
//// //copy文件夾到 tmp/www 路徑下
//// [KFileManger copyMissingFile:directoryPath toPath:tmpWWW];
////
//// // 字符 tmp/www/FeedbackH5/pages/feedback.html 全路徑
//// NSString *tmpWWWFeedback = [tmpWWW stringByAppendingString:@"/FeedbackH5/pages/feedback.html"];
////
//// //tmp 操作仍侥,字符轉(zhuǎn)換成URL
//// NSURL *feedbackURL = [NSURL fileURLWithPath:tmpWWWFeedback];
////
//// //WKWebView加載
//// [_webView loadRequest:[NSURLRequest requestWithURL:feedbackURL]];
//
//// WKWebView *webView =[[WKWebView alloc]initWithFrame:CGRectMake(0, 0, 300, 400)];
//
//// [self.view addSubview:webView];
//
//// [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]]];
//
////// url __NSCFString * @"file:///Users/zhoubo5/jdflutter_webview/dart_2_js_web_example/build/web/index.html" 0x000060000263ec30
//// NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
////
//// NSString *path = [document stringByAppendingString:@"/park.html"];
////
//// NSArray *LibraryArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
////
//// NSString *CachesPath = [[LibraryArray lastObject] stringByAppendingPathComponent:@"Caches"];
////
//// NSString *accessURLStr = [[[LibraryArray lastObject] stringByAppendingPathComponent:@"Caches"] stringByAppendingPathComponent:@"/web"];
//// NSURL *accessURL = [NSURL fileURLWithPath:accessURLStr];
//// [_webView loadFileURL:pathURL allowingReadAccessToURL:accessURL];
//
//// NSData *htmlData = [[NSData alloc] initWithContentsOfFile:@"/Users/zhoubo5/jdflutter_webview/dart_2_js_web_example/build/web/index.html"];
//// NSData *htmlData = [[NSData alloc] initWithContentsOfFile:path];
////
//// NSURL* nsUrl3 = [NSURL URLWithString:@"file:///Users/zhoubo5/Library/Developer/CoreSimulator/Devices/7FF29F20-5129-4931-8F79-A95C70CF2895/data/Containers/Data/Application/3DB61D34-D6DC-4CDB-BA2B-305775ECDDCE/Documents/web"];
//
//// if (@available(iOS 9.0, *)) {
//// [_webView loadData:htmlData MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:nsUrl3];
//// } else {
//// // Fallback on earlier versions
//// }
//
// NSLog(@"zb....%@",@"loadFileURL");
// }
// return true;
//}