To load a Web page on a local or remote server by using UIWebView

Source: Internet
Author: User



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.