Painted levels: being fostered fostered fostered

Tags: “iOS URL encoding” “URL # changed %23” “URL # inaccessible


Share with readers a problem the author encountered in a project where the URL encoding # changed to %23 and could not load.

In a project, you need to load a local HTML file with WKWebView. The path is:

Printing description of pagePath:

/var/mobile/Containers/Data/Application/02EA4A1C-D15D-4B03-A12C-46F097F655E8/Documents/corp/7723038392975868/MiniApp/772 3038392986376/page.html#/pages/home/index

The loading code is as follows:

NSURL *url = [NSURL fileURLWithPath:pagePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
Copy the code

When I run the code, I find that the webView cannot load the file. Is there something wrong with the path itself? The author prepared the page.html dependent resources on the Mac, then tested them in Chrome and found they were accessible.

Is there something wrong with the URL of the final load of the webView? The author begins breakpoint debugging.

  • printpagepathResults:

Printing description of pagePath:

/var/mobile/Containers/Data/Application/23A57DC2-7BAD-40C3-BDF8-AA8355284706/Documents/corp/7723038392975868/MiniApp/772 3038392986376/page.html#/pages/home/index

  • printurlResults:

Printing description of url:

file:///var/mobile/Containers/Data/Application/23A57DC2-7BAD-40C3-BDF8-AA8355284706/Documents/corp/7723038392975868/Mini App/7723038392986376/page.html%23/pages/home/index

I pasted the URL into Chrome and found that it was indeed inaccessible. After careful comparison, # in pagePath becomes %23 after [NSURL fileURLWithPath:]. Guess there is a URL encoding problem, so use the following code to try to solve the problem:

pagePath = [pagePath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
Copy the code

However, it doesn’t work and # is still encoded as %23. The authors speculated that they were using the wrong encoding configuration for pagePath, so they tried various encoding configurations (including some found on the Web), but none of them worked.

#/pages/home/index is removed from pagePath and loaded in webView. And found that, In – (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation callback, the URL Po came out of navigation #/pages/home/index As follows:

Printing description of webView.URL:

file:///var/mobile/Containers/Data/Application/23A57DC2-7BAD-40C3-BDF8-AA8355284706/Documents/corp/7723038392975868/Mini App/7723038392986376/page.html#/pages/home/index

HTML #/pages/home/index was redirected after it was accessed. That’s not the point, but it does show that the webView itself is fine and supports the # character. So the problem is that the # becomes %23.

After repeated testing, the authors found that the [NSURL fileURLWithPath:] method changed # in pagePath to %23, while [NSURL URLWithString:] did not. Careful testing and observation showed that the latter consistently lacked the file:// prefix. Well, here’s a temporary solution:

pagePath = [@"file://" stringByAppendingString:pagePath];
NSURL *url = [NSURL URLWithString:pagePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
Copy the code

Run the code,pagePathIt can now be loaded and accessed properly. The author always felt that this solution was a little “wild”, but after consulting the data, he still could not find another solution, so let it be.

Ways to focus on us are:

QiShare(GitHub) QiShare(CocoaChina) QiShare(StackOverflow) QiShare(wechat)

Xcode adjusts navigation directory font size b

Swift 5.1 (21) – Generic Swift 5.1 (20) – Protocol Swift 5.1 (19) – Extended Swift 5.1 (18) – Nested Type Swift 5.1 (17) – Type conversion and pattern matching Download and export project run logs, Flutter Platform Channel, and source code analysis