2016-09-08 11 views
0

WKWebViewのcssとjsファイルを使用してcacheDirectoryからHTMLファイル(file:////Library/Cache/sample1_web/index.html)をロードします。しかし、それはWKWebViewのload 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と成功ロードHTMLとcssとjsをロードしようとしました。

そして、私は確認してください。

からhttps://forums.xamarin.com/discussion/61891/wkwebview-load-local-content-with-loadfileurl - WkWebView won't loadHTMLstring

+0

このAPIを試してみてください 'FUNC loadFileURL(_ URL:URL、 allowingReadAccessTo readAccessURL:URL) - ?> WKNavigation' ベースURLへのアクセスを与えます。 – DocForNoc

答えて

0

あなたのリソースはzip圧縮され、そしてあなただけ(ないファイルシステム上で)それらをメモリでを解凍した場合はHTMLをロードし、あなたのJavaScript CSSファイルは圧縮されたままであり、<script src="js/script.js">タグ内のパスは、srcパスが存在しないため有効ではありません。

ディレクトリ全体を解凍して、すべてのリソースを互いに見えるようにする必要があります。

関連する問題