带有片段 ID 的内部 URL
我在主包中有一个大的 .htm
文件,我希望加载到 webview 中。在我将片段 id 附加到 URL 末尾之前没有问题。我希望按下按钮时 webview 在锚点 id #c
处打开。
顺便说一句:如果我创建一个 URL http://www.mydomain.com/database.htm#c< /a> 并从互联网加载它。一切一切都好。
我尝试了几种方法。两者都失败了。我猜测服务器端会解析互联网上的 URL,但 iPhone 没有这种能力。有人有建议吗?如果可能,请通过示例进行明确说明。我是一个菜鸟并且靠我自己。
难住了
NSString *path = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
NSString *pathWithFrag = [NSString stringWithFormat:@"%@%@%",path, @"#c"];
NSURL *baseURL = [ NSURL fileURLWithPath:pathWithFrag];
[spinner2 loadRequest:[NSURLRequest requestWithURL:baseURL]];
[super viewDidLoad];
或
NSString *path = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
NSURL *baseURL = [ NSURL fileURLWithPath:path];
NSURL *newURL = [baseURL URLByAppendingPathComponent:@"#c"];
[spinner2 loadRequest:[NSURLRequest requestWithURL:newURL]];
[super viewDidLoad];
I have a large .htm
file in the main bundle I wish to load into a webview. No problem until I append a fragment id to the end of the URL. I want the webview to open at an anchor id #c
on a button press.
BTW: If I create a URL http://www.mydomain.com/database.htm#c and load this from the Internet. All all is good.
I have tried several approaches. Both have failed. I am guessing the server side does the parsing of the URL on the internet, but the iPhone does not have that capability. Does anyone have a suggestion? Please be explicit with examples if possible. I am a noob and on my own.
Stumped
NSString *path = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
NSString *pathWithFrag = [NSString stringWithFormat:@"%@%@%",path, @"#c"];
NSURL *baseURL = [ NSURL fileURLWithPath:pathWithFrag];
[spinner2 loadRequest:[NSURLRequest requestWithURL:baseURL]];
[super viewDidLoad];
OR
NSString *path = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
NSURL *baseURL = [ NSURL fileURLWithPath:path];
NSURL *newURL = [baseURL URLByAppendingPathComponent:@"#c"];
[spinner2 loadRequest:[NSURLRequest requestWithURL:newURL]];
[super viewDidLoad];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来可能是 NSURL url 转义“#”字符的问题:http://9mmedia。 com/blog/?p=299
按照该博客文章中的方法,类似这样的方法可能会起作用:
It looks like it may an issue with NSURL url-escaping the "#" character: http://9mmedia.com/blog/?p=299
Following the method on that blog post, something like this might work:
pathWithFrag
格式中额外的%
可能会导致问题:尝试@"%@%@"
,而不是@"%@ %@%"
The extra
%
in thepathWithFrag
format may be causing problems: try@"%@%@"
, not@"%@%@%"