我想从cacheDirectory中加载html文件(file:////Library/Cache/sample1_web/index.html),并在WKWebView中使用css和js文件。但是,它不是在WKWebView中加载css和js文件。如何使用WKWebView从CacheDirectory加载css和js文件?
我加载使用方法:loadHTMLString:基本URL
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_webView = [[WKWebView alloc] initWithFrame:self.view.frame];
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sample1_web.zip"];
NSString *destPath = [self getCachePath];
if ([SSZipArchive unzipFileAtPath:filePath toDestination:destPath]) {
NSLog(@"SUCCESS unzip!");
NSError *erno = nil;
NSString *urlHTML = [[self getCachePath] stringByAppendingString:@"/sample1_web/index.html"];
NSString *contentFile = [NSString stringWithContentsOfFile:urlHTML encoding:NSUTF8StringEncoding error:&erno];
NSURL *urlBase = [[NSBundle bundleWithPath:[[self getCachePath] stringByAppendingPathComponent:@"sample1_web"]] bundleURL];
NSLog(@"@%@", urlBase);
NSLog(@"@%@", contentFile);
if (erno == nil) {
[_webView loadHTMLString:contentFile baseURL:urlBase];
}else
NSLog(@"%@", erno);
} else {
NSLog(@"FAILED unzip...");
}
[self.view addSubview:_webView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSString *)getCachePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
而且,完美的负载HTML,但不与CSS和JS。问题是什么?我试图用UIWebView加载这个HTML并成功加载CSS与CSS和JS。
我检查:
- https://forums.xamarin.com/discussion/61891/wkwebview-load-local-content-with-loadfileurl - WkWebView won't loadHTMLstring
试试这个API'FUNC loadFileURL(_网址:网址, allowingReadAccessTo readAccessURL:URL) - > WKNavigation' 给访问自己的基本网址? – DocForNoc