是否有可能发送一个哈希标签到UIWebView中的文件URL?

我想从一个文件加载一个html页面,并附加一个哈希标签。 这可能吗?

我努力了

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"html"]; NSURL *fileUrl = [NSURL fileURLWithPath:[filePath stringByAppendingFormat:@"#hashtag"]]; [self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]]; NSLog(@"fileUrl = %@, reachable? %d", fileUrl, [fileUrl checkResourceIsReachableAndReturnError:nil]); 

但是这试图寻找文件someFile.html%23hashtag ,这是找不到的。 有没有办法在创buildNSURL对象后添加散列?

我也尝试加载文件到一个string,并使用loadHTMLString

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"html"]; NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; [self.webView loadHTMLString:fileContents baseURL:[NSURL URLWithString:@"http://someFile.html#hashtag"]]; 

这里的哈希标签确实工作,但我的HTML内的HTML引用不起作用。 从这个方法的后续问题将是,我如何引用JavaScript文件从一个UIWebView中的string加载的HTML,即什么是基本的url?

我可以想到的一个黑客就是把所有的JavaScript文件内联在HTML中,并把它作为一个string加载,但是我想一定有更好的办法!

我还没有尝试过,但如何正常加载文件没有hashtag和UIWebViewDelegate实现这样的事情?

 - (void)webViewDidFinishLoad:(UIWebView *)webView { [webView stringByEvaluatingJavaScriptFromString:@"window.location.href = '#hashtag';"]; } 

参考文献:

  • UIView把你带到链接
  • 以编程方式滚动到锚标签

至于%23的问题,我不认为Scott Kohlert的replace解决scheme是好的。

下面的解决scheme似乎更好,我只是从这里复制它

 NSURL *baseUrl = [NSURL fileURLWithPath:stringUrl]; NSURL *fullURL = [NSURL URLWithString:@"#jumpto" relativeToURL:baseUrl]; 

有一点点题外话,我发现在iOS 6.0,6.1上的Safari的行为与在iOS 5.1和其他桌面浏览器(包括OSX的Safari)上的行为差别很大。 对于我的特定文档之一,无论是在iOS 6.0或6.1的UIWebview或移动Safari上,在第一次加载时,页面不滚动到正确的位置,重新加载将修复它。 也许这与HTML的一部分是由JavaScriptdynamic生成的事实有关。 有任何想法吗?

这是我在我的代码中做的。 我将哈希标记附加到NSString,然后使用fileURLWithPath将其变为NSURL。 然后,我将%23的所有实例都replace成#。 它的代码在下面的代码中解释,但让我知道,如果你有任何问题。

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"html"]; NSString *filePathWithHash = [NSString stringWithFormat:@"%@#yourDesiredHashTagHere",filePath]; NSURL *theURL = [NSURL fileURLWithPath:filePathWithHash]; //NSURL turns the # into %23 when using fileURLWIthPath. So we need to change it back: NSString *finalURLString = [[NSString stringWithFormat:@"%@",theURL] stringByReplacingOccurrencesOfString:@"%23" withString:@"#"]; //Then we need to change it back to an NSURL NSURL *finalURL = [NSURL URLWithString:finalURLString]; //And finally we load this in the webView [theWebView loadRequest:[NSURLRequest requestWithURL:finalURL]]; 

您应该能够截取NSURLRequest ,将其转换为NSMutableURLRequest ,然后按如下所示更改URL。 所有这些都会在shouldStartLoadWithRequest发生。 确保你设置了UIWebView委托。

 - (void) viewDidLoad { self.webView.delegate = self; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"html"]; NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { //Make sure the request is mutable if(![request isKindOfClass:[NSMutableURLRequest class]]) [NSException raise:@"Need to change the request, but can't" format:@""]; NSMutableURLRequest* mutableRequest = (NSMutableURLRequest*)request; NSString* newUrl = [NSString stringWithFormat:@"%@#hashtag", [[request URL] absoluteString]]; [mutableRequest setURL:[NSURL URLWithString:newUrl]]; return YES; } 

我没有遇到任何请求不可变的情况,但谁知道。

或者您可能需要在原始URL中设置哈希(就像您正在做的那样),然后在shouldStartLoadWithRequest第一个出现的%23replace为#

除了@ xiang的答案。 在swift版本下面,如果你正在使用应用中的本地html文件:

 let urlString = "relative/path/to/your/index.html" let anchorString = "#your-anchor" let baseURL:NSURL? = NSBundle.mainBundle().URLForResource(urlString, withExtension: nil, subdirectory: nil) if let url:NSURL? = NSURL(string: anchorString, relativeToURL: baseURL) { // Do something with your URL } 

我想你必须在创build文件URL后添加散列

如果你改变,它会工作吗?

 NSURL *fileUrl = [NSURL fileURLWithPath:[filePath stringByAppendingFormat:@"#hashtag"]]; 

像这样的东西:

 NSString *urlString = [[NSURL fileURLWithPath:filePath] absoluteString]; NSURL *fileUrl = [NSURL URLWithString:[urlString stringByAppendingString:@"#hashtag"]];