標籤:ror ase bundle base 網頁 data- cep err content
大家都知道,使用UIWebView載入本地或遠程server上的網頁,sdk提供了三個載入介面:- (void)loadRequest:(NSURLRequest *)request; - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;- (void)loadRequest:(NSURLRequest *)request; 這個介面一般用於載入url所指定的某個遠程server網頁,事實上它也能用來載入本地網頁資源。//載入遠程網頁[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];//載入本地網頁NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kline71" ofType:@"html" inDirectory:@"XiangJie"];[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];注意:網頁可能會使用其它的片資源,css樣式檔案,loadRequest會在網頁的當前檔案夾下(如本例中的XiangJie檔案夾下尋找)- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;這個介面用於直接載入html代碼。假設html直接寫在了代碼中,推薦使用這樣的方法。當然你也能夠先從本地讀取html代碼,然後載入。
請注意baseURL地址檔案夾要正確,否則html中引用的資源是找不到的。NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kline71" ofType:@"html" inDirectory:@"XiangJie"];NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"XiangJie/img"];[_myWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString:baseURL]];- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;這個介面用於載入html檔案。
MIMEType值一般為"text/html"。相同。請注意baseURL地址檔案夾要正確。否則html中引用的資源是找不到的。NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kline71" ofType:@"html" inDirectory:@"XiangJie"];NSData *data = [NSData dataWithContentsOfFile:filePath];NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"XiangJie/img"];[self.myWebView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:baseURL]];
使用UIWebView載入本地或遠程server上的網頁