在 xCode iOS 中使用超文本
我想为 iOS 应用程序制作一组帮助页面,并且拥有超文本目录似乎是合乎逻辑的。
这可能吗?如果可以,我如何将页面链接包装在 OBj-C 中?
谢谢..
我在卡住的地方添加了一些代码。我使用Apple的TransWeb示例作为基础..
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"page1" ofType:@"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.webView loadHTMLString:htmlString baseURL:nil];
这就是我在html文件中设置链接的方式..
<html>
<p>
Located in the Sierra Nevada foothills of <a href = "file://page2.html">California</a>
in the Sierra Nevada ...........
</p>
感谢Caleb给我正确的答案..
我已经更改了我的webView loadRequest如下:
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"page1" ofType:@"html"]isDirectory:NO]]];
和因此更改了 html 文件:
<a href = "page2.html">California</a>
I would like to make a set of help pages for an iOS app, and it seems logical to have a hypertext table of contents.
Is this possible, and if so, how do I wrap the page links in OBj-C?
Thanks..
I'm adding some code where I'm stuck. I'm using Apple's TransWeb example as a base..
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"page1" ofType:@"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.webView loadHTMLString:htmlString baseURL:nil];
This is how I've set up the links in the html files..
<html>
<p>
Located in the Sierra Nevada foothills of <a href = "file://page2.html">California</a>
in the Sierra Nevada ...........
</p>
Thanks to Caleb for giving me the right answer..
I've changed my webView loadRequest as follows:
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"page1" ofType:@"html"]isDirectory:NO]]];
and changed the html file thus:
<a href = "page2.html">California</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然——只需使用 UIWebView 来显示您的 HTML 页面。使用相对 URL 在页面之间进行链接。
更新:我在您发布的代码中发现的一个问题是您的网址不正确。您有一个如下所示的文件 URL:
file://page2.html
。这是不正确的——如果您想要一个相对于主机根目录的 URL,您可以使用:file:///page2.html
。该 URL 省略了主机,这意味着应使用与当前页面相同的主机。如果您想让 URL 相对于当前页面的主机和目录,请忽略整个方案:page2.html
。因此,您的锚标记可能如下所示:另一件需要考虑的事情是您可能不需要费心使用 NSFileHandle。只需将根页面的数据直接加载到字符串中即可:
Sure -- just use a UIWebView to display your HTML pages. Use relative URLs to link between pages.
Update: One problem I see in the code you posted is that your URL is incorrect. You've got a file url like this:
file://page2.html
. That's incorrect -- if you want an url that's relative to the host's root, you can use:file:///page2.html
. That URL omits the host, which implies that the same host as the current page should be used. If you want to make the URL relative to both the host and the directory of the current page, leave off the whole scheme:page2.html
. So your anchor tag could look like:Another thing to consider is that you probably don't need to bother with the NSFileHandle. Just load the root page's data directly into a string: