使用UIWebView載入本地或遠程server上的網頁

來源:互聯網
上載者:User

標籤: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上的網頁

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.