2011-12-02 287 views
6

我是新來的iOS編程,並試圖找出什麼loadHTMLString:baseURL:真的,但我找不到滿意的解釋。蘋果網站只是說:loadHTMLString是什麼:baseURL:

設置主頁面內容和基地址。

有人可以請更詳細地解釋這個給我嗎?

回答

3

這是主要如何在webView中加載內容。無論是從本地html文件或通過網址。

//this is to load local html file. Read the file & give the file contents to webview. 
[webView loadHTMLString:someHTMLstring baseURL:[NSURL URLWithString:@""]]; 

//if webview loads content through a url then 
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]] 
+0

如果我想加載本地文件,我可以用@ index.html替換@「」。對? – MACMAN

4

我敢肯定的是,在基本URL一樣使用常規的網頁加載正確正在使用相對鏈接引用ressources。現在的問題是,如何將該基本URL設置爲應用程序目錄中的特定文件夾。

1
- (void) loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL; 

用來加載本地HTML文件,參數字符串表示的HTML文件的內容,如果您HTML文件包含一些href標籤使用相對路徑,你應該設置參數baseUrlHTML文件的基地址,或將其設置爲nil

NSString *cachePath = [self cachePath]; 
NSString *indexHTMLPath = [NSString stringWithFormat:@"%@/index.html", cachePath]; 
if ([self fileIsExsit:indexHTMLPath]) { 
    NSString *htmlCont = [NSString stringWithContentsOfFile:indexHTMLPath 
                  encoding:NSUTF8StringEncoding 
                   error:nil]; 
    NSURL *baseURL = [NSURL fileURLWithPath:cachePath]; 
    [self.webView loadHTMLString:htmlCont baseURL:baseURL]; 
} 

- (NSString *)cachePath 
{ 
    NSArray* cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    return [cachePath[0] stringByAppendingPathComponent:@"movie"]; 
}