带有片段 ID 的内部 URL

发布于 2024-11-04 07:07:36 字数 1069 浏览 0 评论 0原文

我在主包中有一个大的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

や三分注定 2024-11-11 07:07:36

看起来可能是 NSURL url 转义“#”字符的问题:http://9mmedia。 com/blog/?p=299

按照该博客文章中的方法,类似这样的方法可能会起作用:

NSString *path = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSURL *fullURL = [NSURL URLWithString:@"#c" relativeToURL:baseURL];
[spinner2 loadRequest:[NSURLRequest requestWithURL:fullURL]];
[super viewDidLoad];

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:

NSString *path = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSURL *fullURL = [NSURL URLWithString:@"#c" relativeToURL:baseURL];
[spinner2 loadRequest:[NSURLRequest requestWithURL:fullURL]];
[super viewDidLoad];
深居我梦 2024-11-11 07:07:36

pathWithFrag 格式中额外的 % 可能会导致问题:尝试 @"%@%@",而不是 @"%@ %@%"

The extra % in the pathWithFrag format may be causing problems: try @"%@%@", not @"%@%@%"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文