2013-03-09 179 views
0

我有点卡住,HTML文件的Xcode可可按钮...打开新窗口

我有我的应用程序写的,我现在只需要拥有下一个打开HTML文件是在一个按钮我的应用程序资源文件夹。

所以如果你点击按钮,html文件将在新窗口或Safari浏览器中打开。无论如何,这是最简单的方法。

任何人可以帮助我吗?

+0

http://stackoverflow.com/questions/3885641/cocoa-webview-loding-a-local-html-page – Shashank 2013-03-09 09:47:02

回答

0

您需要从路径中读取文件。

如果它在资源文件夹(即NSBundle)中,那么确定,否则获取路径。

阅读并显示它。

你可以做到这一点有两种方式,第二种方式是使用NSAppleScript,这对于沙盒应用程序来说并不好。

第一种方法是在webView中显示它。

NSString *path=[[NSBundle mainBundle] pathForResource:@"index" 
               ofType:@"html"/*or htm as per your file extension*/ 
              inDirectory:@"yourDirectoryName"]; 
NSURL *url=[NSURL fileURLWithPath:path]; 
NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url]; 
[[webView mainFrame] loadRequest:urlRequest]; 
0

在这里你去:

NSString *path = [[NSBundle mainBundle] pathForResource:@"your file name" ofType:@"html"]; 
NSURL *baseURL = [[NSURL alloc] initFileURLWithPath:path isDirectory:YES]; 
NSString *html = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:path] encoding:NSUTF8StringEncoding error:nil]; 
[webview setDelegate:self]; 
webview.dataDetectorTypes = UIDataDetectorTypeAll; 
[webview loadHTMLString:html baseURL:baseURL];