是否可以将哈希标记发送到UIWebView中的文件URL? [英] Is it possible to send a hash tag to a file url in UIWebView?

查看:96
本文介绍了是否可以将哈希标记发送到UIWebView中的文件URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件加载一个html页面,并为其添加一个哈希标记。这可能吗?

I want to load a html page from a file, and append a hash tag to it. Is this possible?

我试过了

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 ,找不到。有没有办法在创建 NSURL 对象后添加哈希?

but this tries to look for the file someFile.html%23hashtag, which can't be found. Is there a way to add the hash after the NSURL object is created?

我也试过加载将文件转换为字符串并使用 loadHTMLString

I've also tried loading the file into a string and using 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中的javascript引用不起作用。这个方法的后续问题是,如何从UIWebView中作为字符串加载的html引用javascript文件,即什么是基本URL?

Here the hash tag does work, but my javascript references inside the html don't work. A follow on question from this approach would be, how do I reference javascript files from html loaded in as a string in a UIWebView, ie, what is the base url?

我能想到的一个黑客就是将我的所有javascript文件内联到html中并将其加载为字符串,但我认为必须有更好的方法!

A hack I can think of is to just put all my javascript files inline in the html and load it as a string, but I'm thinking there must be a better way!

推荐答案

我没试过这个但是如何在没有hashtag的情况下正常加载文件并用这样的东西实现UIWebViewDelegate?

I've not tried this but how about loading the file normally without the hashtag and implementing the UIWebViewDelegate with something like this?

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

参考文献:

  • UIView that takes you to anchor link
  • Programmatically scroll to an Anchor Tag

这篇关于是否可以将哈希标记发送到UIWebView中的文件URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆