使用锚点在uiwebview中加载本地html5

时间:2013-01-06 12:37:45

标签: ios html5 uiwebview

我有以下代码在iOS应用中加载本地html文件。

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

以上工作,但我需要加载一个锚点,因为这是一个嵌入在UIWebview中的单页面Web应用程序。

基本上我如何加载index.html#settings而不仅仅是index.html。

1 个答案:

答案 0 :(得分:0)

This为我工作。

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

[NSURL fileURLWithPath:basePath]中将#转换为%23,但在URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL;中将#转换为%23。