-(Ibaction) testloadhtmlsting: (ID) Sender {//Set the basic path of the home file//file named "index.html"//[NSBundle Mainbundle] is to get the current project address NSString *htmlpath = [[NSBundle mainbundle]pathforresource: @ "index" oftype:@ "html"]; Get HTML resource path Nsurl *bundleurl = [Nsurl fileurlwithpath:[[nsbundle mainbundle] Bundlepath]]; Nserror *error = nil; You must specify a character set when you specify the coded character set, and then load the Htmlpath into the encoding//loading WebView! Storing the contents of HTML in nsstring nsstring *html = [[NSString alloc] Initwithcontentsoffile:htmlpath Encoding:nsutf8stringencodin G error:&error]; if (Error = = nil) {//loadhtmlstring used to get the path of the HTML [Self.webview loadhtmlstring:html Baseurl:bundleurl]; }}-(Ibaction) Testloaddata: (ID) Sender {//Set home page file! The basic path to load the home page data via an HTML string nsstring *htmlpath = [[NSBundle Mainbu Ndle] pathforresource:@ "index" oftype:@ "html"]; Gets the resource path of the home file Nsurl *bundleurl = [Nsurl fileurlwithpath:[[nsbundle mainbundle] Bundlepath]]; Nserror *error = nil; Here is nsdata nsdata *htmldata = [[NSData alloc] initwithcontentsoffile:htmlpath]; if (Error = = nil) {//because it is nsdata you need to specify the character set UTF-8 [Self.webview loaddata:htmldata mimetype:@ "text/html" Textenc odingname:@ "UTF-8" baseurl:bundleurl]; }}-(ibaction) Testloadrequest: (ID) Sender {//string saved as Nsurl nsurl *url = [Nsurl urlwithstring:@] Http://www.51work6 . com "]; Initiates an asynchronous request nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; [Self.webview Loadrequest:request]; Self.webView.delegate = self;}
How to use WebView