【发布时间】:2016-09-08 07:32:29
【问题描述】:
我想在 WKWebView 中使用 css 和 js 文件从 cacheDirectory 加载 html 文件 (file:////Library/Cache/sample1_web/index.html)。但是,它不会在 WKWebView 中加载 css 和 js 文件。
我用方法加载:loadHTMLString:baseURL
- (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 和 js 加载 html。
我检查一下:
-https://forums.xamarin.com/discussion/61891/wkwebview-load-local-content-with-loadfileurl -WkWebView won't loadHTMLstring
【问题讨论】:
-
试试这个 API
func loadFileURL(_ URL: URL, allowingReadAccessTo readAccessURL: URL) -> WKNavigation?访问您的基本网址。
标签: ios objective-c uiwebview webkit wkwebview