As you all know, using UIWebView to load a Web page on a local or remote server, the SDK provides three loading interfaces:-(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; This interface is typically used to load a remote server Web page specified by the URL, but it can also be used to load local Web page resources. Load Remote Web page [self.mywebview loadrequest:[nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://www.baidu.com"]] ;//load local page NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "kline71" oftype:@ "html" indirectory:@ "Xiangjie "]; [Self.mywebview loadrequest:[nsurlrequest Requestwithurl:[nsurl Fileurlwithpath:filepath]]; Note: Web pages may use other slices of resources, CSS style files, Loadrequest will be found in the current directory of the Web page (as in the Xiangjie directory in this example)-(void) loadhtmlstring: (NSString *) string BaseURL: (Nsurl *) BaseURL; This interface is used to load HTML code directly. This approach is recommended if HTML is written directly in the code. Of course you can also read the HTML code locally and then load it. Note that the BaseURL address directory is correct, otherwise the resources referenced in the HTML are not found. 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; This interface is used to load HTML files. The mimetype value is generally "text/html". Also, note that the BaseURL address directory is correct, otherwise the resources referenced in the HTML are not found. 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] ];
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
To load a Web page on a local or remote server by using UIWebView