2013-03-09 307 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];