Search code examples
ioshtmluiwebview

loading local html5 in uiwebview with anchor


I have the following code to load a local html file in an iOS app.

[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

The above works, however I need to load an anchor as this is a single page web app embedded in a UIWebview.

Basically how do I load index.html#settings for instance instead of just index.html.


Solution

  • This worked for me.

    NSString *basePath = [[NSBundle mainBundle] pathForResource:@"page.html" ofType:nil];
    NSURL *baseURL = [NSURL fileURLWithPath:basePath];
    NSURL *fullURL = [NSURL URLWithString:@"#anchor1" relativeToURL:baseURL];
    // Now do whatever with this fullURL
    

    The # is converted to %23 in [NSURL fileURLWithPath:basePath], but URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL; not.