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.