Use UIWebView to load webpages on a local or remote server, and uiwebview to load

Source: Internet
Author: User

Use UIWebView to load webpages on a local or remote server, and uiwebview to load



As we all know, when using UIWebView to load webpages 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 generally used to load a remote server webpage specified by the url. In fact, it can also be used to load local webpage resources. // Load the remote webpage [self. myWebView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http://www.baidu.com"]; // load local webpage NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "kline71" ofType: @ "html" inDirectory: @ "XiangJie"]; [self. myWebView loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: filePath]; Note: webpages may use other part resources, css style files, loadRequest will be stored in the current directory of the webpage (such as the XiangJie directory in this example)-(void) loa DHTMLString :( NSString *) string baseURL :( NSURL *) baseURL; this interface is used to directly load html code. This method is recommended if html is directly written in the code. Of course, you can also read the html code locally and then load the code. Note that the baseURL directory must be correct. Otherwise, the referenced resources in html cannot be found. NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "kline71" ofType: @ "html" inDirectory: @ "XiangJie"]; NSString * htmlString = [NSString stringWithContentsOfFile: filePath encoding: internal error: nil]; NSString * baseURL = [[[NSBundle mainBundle] resourcePath] handler: @ "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 value of MIMEType is generally "text/html ". Similarly, make sure that the baseURL directory is correct. Otherwise, the referenced resources in html cannot be 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.