How to use NSURL fileURLWithPath with fragment?

I have an IOS project with html files shown in webview. These html files have different sections that correspond to the fragments (for example, index.html # section1, index.html # section2) that I would like to upload to webview. Unfortunately, use [NSURL fileURLWithPath:url]does not work with fragments. #converts to %23in url and file not found. If I use a method [NSURL URLWithString:url], it works, but this method cannot load local resources.

Is there a way for a webview to load a local resource url using a snippet?

+5
source share
2 answers

, URL- , , :

[webView stringByEvaluatingJavaScriptFromString:@"window.location.hash = '#section2';"];
+3

, Apple . , , :

NSURL *url = [NSURL fileURLWithPath:@"/full/path/to/index.html"];
NSString *fragment = @"#section1";
url = [NSURL URLWithString:fragment relativeToURL:url];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
+3

All Articles