如何加载包含散列片段“#"的 NSURL;使用 UIWebView? [英] How to load NSURL which contains hash fragment "#" with UIWebView?
问题描述
给定一个本地 URL 地址,如 index.html
Given a local URL address like index.html
现在我需要使用 UIWebView
在 iPad 中加载它.我按照以下步骤操作:
Now I need to use UIWebView
to load it in iPad. I followed these steps:
创建 NSURL
Create NSURL
NSURL *url = [NSURL fileURLWithPath:@"http://mysite.com#page1"];
使用 UIWebView 加载本地 HTML/JS/CSS 等
Load with UIWebView for local HTML/JS/CSS etc
[webView loadRequest:[NSURLRequest requestWithURL:url]];
但是不行,因为#"被转换成%23",所以URL字符串是
But it doesn't work, because "#" is converted to "%23", so the URL string is
http://mysite.com%23page1
我的问题是,如何解决这个自动转换问题并让 UIWebView
访问包含哈希片段#"的 URL?
My question is, how to fix this auto-conversion issue and let UIWebView
access the URL which contains the hash fragment "#"?
推荐答案
用户 URLWithString 将片段附加到您的网址,如下所示:
User URLWithString to append fragment to your url, like this:
*NSURL *url = [NSURL fileURLWithPath:htmlFilePath];
url = [NSURL URLWithString:[NSString stringWithFormat:@"#%@", @"yourFragmentHere"] relativeToURL:url];*
希望它会有所帮助:)
Swift 3 版本:
Swift 3 version:
var url = URL(fileURLWithPath: htmlFilePath)
url = URL(string: "#yourFragmentHere", relativeTo: url)
这篇关于如何加载包含散列片段“#"的 NSURL;使用 UIWebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!