2016-09-08 132 views
0

我想从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

+0

试试这个API'FUNC loadFileURL(_网址:网址, allowingReadAccessTo readAccessURL:URL) - > WKNavigation' 给访问自己的基本网址? – DocForNoc

回答

0

如果你的资源是压缩的,你只能对其进行解压缩在内存(而不是文件系统)加载HTML的时候,你的JavaScript并且CSS文件保持压缩状态,并且<script src="js/script.js">标记中的路径将无效,因为src路径不存在。

您需要解压缩整个目录,以便您的所有资源都可以互相看到。

相关问题