[英]How to load only HTML+CSS+JS and stop loading images on Cocoa WebView
[英]How to load local html files with images, js and css on WebView Cocoa / Mac OS
我有代碼可以在iOS上運行。 使用我在這里和網絡上找到的任何東西,我設法在某個地方制作Cocoa Mac Os版本,但圖像不會加載。 CSS和Javascript似乎也沒有加載。 HTML的目錄被添加到Resources組,但它被添加為Folder References(藍色文件夾),這使得Xcode尊重HTML App的目錄結構。
這是我正在嘗試使用的代碼:
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"
inDirectory:@"/Patient_eMMR_HTML5" ];
NSString *html = [NSString stringWithContentsOfFile:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[[webView mainFrame] loadHTMLString:html baseURL:[NSURL fileURLWithPath:
[NSString stringWithFormat:@"%@/Patient_eMMR_HTML5/",
[[NSBundle mainBundle] bundlePath]]]];
這是我使用上面使用的代碼的代碼的iOS版本:
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"
inDirectory:@"/Patient_eMMR_HTML5" ];
NSString *html = [NSString stringWithContentsOfFile:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[webView loadHTMLString:html
baseURL:[NSURL fileURLWithPath:
[NSString stringWithFormat:@"%@/Patient_eMMR_HTML5/",
[[NSBundle mainBundle] bundlePath]]]];
任何幫助是極大的贊賞。 謝謝。
如果您通過NSURLRequest
而不是字符串加載HTML,則無需使用基本URL:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"
inDirectory:@"Patient_eMMR_HTML5"];
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[[webView mainFrame] loadRequest:request];
請注意,您應僅在‑pathForResource:…
方法中指定目錄名稱,不要在其前面添加正斜杠。 假設命名目錄以root包的目錄為根。
請注意, UIWebView
還有一個‑loadRequest:
方法,因此您可以在iOS上使用幾乎相同的代碼。
這適用於Mac OSX上的resourceURL
[webView loadHTMLString:htmlContent baseURL:[[NSBundle mainBundle] resourceURL]]
mserin,這個問題針對的是Mac OS X的Cocoa Framework。您正在使用UIWebView,它是UIKit for Cocoa Touch的一部分。 如果你想為iPhone / iPad構建這個,你應該看看這篇文章。
如果您正在嘗試這樣做,那么使用我們在這里使用的代碼,但您需要包含WebKit框架,如下所示:
#import <WebKit/WebKit.h>
然后繼承WebView,而不是UIWebView:
WebView *webView;
有這樣的屬性:
@property (nonatomic, retain) IBOutlet WebView *webView;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.