WKWebViewはローカルファイルをロードします。ブログ投稿iOS_WKWebViewを参照して、ローカルWebページをロードできます。
1.プロジェクトにHTMLファイルを追加するHTML LoactionHtml.Bundle
リソースファイルを保存するファイルを作成しましたプロジェクトファイルの入手index.html
方法は以下の通りです。
NSString *path = [[[NSBundle mainBundle] pathForResource:@"LoactionHtml" ofType:@"bundle"] stringByAppendingPathComponent:@"index.html"];
2.次のように/index.html#/pages/activity/create/index
、ローカルバンドルで「#」を含むパスを読み取る必要があり
ます。一般的な書き込み方法に従うと、ロードが失敗したことがわかります。印刷すると、パスの「#」が「%23」にコンパイルされていることがわかります。このように、WKWebViewは対応するパスを正常にロードできません。
NSString *path = [[[NSBundle mainBundle] pathForResource:@"LoactionHtml" ofType:@"bundle"] stringByAppendingPathComponent:@"index.html#pages/activity/create/index"];
NSURL *fileUrl = [NSURL fileURLWithPath:path];
// WKWebView加载fileUrl
[self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]];
3.パスの「#」が「%23」にコンパイルされないようにするにはどうすればよいですか?
3.1。NSURL fileURLWithPath:
を取得する方法を変更するURLWithString:
;
3.2。パスのPath
前に文字列「file://」を接続します。
印刷すると、fileUrlの「#」が「%23」にコンパイルされていないため、正常にロードできます。 。
NSString *path = [[[NSBundle mainBundle] pathForResource:@"LoactionHtml" ofType:@"bundle"] stringByAppendingPathComponent:@"index.html#pages/activity/create/index"];
// 在path前拼接字符串"file://"
NSURL *fileUrl = [NSURL URLWithString:[@"file://" stringByAppendingString:path]];
// WKWebView加载fileUrl
[self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]];
fileUrlは次のように2回出力されます。
// 拼接file://
file:///Users/xxxxxxxxx/index.html#/pages/activity/create/index
// 未拼接file://
file:///Users/xxxxxxxxx/index.html%23/pages/activity/create/index