Webview usage and webview usage
-(IBAction) testLoadHTMLSting :( id) sender {// set the basic path of the home page file // the file name is “index.html "// [NSBundle mainBundle] is used to obtain the current project address NSString * htmlPath = [[NSBundle mainBundle] pathForResource: @ "index" ofType: @ "html"]; // obtain the html resource path NSURL * bundleURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]; NSError * error = nil; // specify the encoding Character Set first, and then load the htmlPath into encoding. // you must specify the character set when loading WebView! // Save html content to NSString * html = [[NSString alloc] initWithContentsOfFile: htmlPath encoding: NSUTF8StringEncoding error: & error]; if (error = nil) {// loadHTMLString is used to obtain the html path [self. webView loadHTMLString: html baseURL: bundleURL] ;}}-(IBAction) testLoadData :( id) sender {// set the homepage file! Using an HTML string to load the home data NSString * htmlPath = [[NSBundle mainBundle] pathForResource: @ "index" ofType: @ "html"]; // obtain the resource path of the home page file NSURL * bundleUrl = [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]; NSError * error = nil; // here is NSData * htmlData = [[NSData alloc] initWithContentsOfFile: htmlPath]; if (error = nil) {// the character set UTF-8 [self. webView loadData: htmlData MIMEType: @ "text/html" textEncodingName: @ "UTF-8" baseURL: bundleUrl] ;}- (IBAction) testLoadRequest :( id) sender {// string to NSURL * url = [NSURL URLWithString: @ "http://www.51work6.com"]; // initiate an asynchronous request NSURLRequest * request = [NSURLRequest requestWithURL: url]; [self. webView loadRequest: request]; self. webView. delegate = self ;}
Webview: Select whether to use the local browser to open
1. generally, you may want to use WebView to open the internal link of a Web page instead of calling a mobile browser. We can use either of the following two methods: (1) set a WebViewClient for WebView, and override the shouldOverrideUrlLoading (WebView view, String url) method. ClassMyWebViewClientextendsWebViewClient {@ OverridepublicbooleanshouldOverrideUrlLoading. loadUrl (url); returntrue ;}} (2) sets a WebViewClient for WebView and overrides the onPageStarted (WebView view, String url, Bitmap favicon) method. Other {@ OverridepublicvoidonPageStarted (WebViewview, Stringurl, Bitmapfavicon) {// TODOAuto-generatedmethodstubsuper.onPageStarted (view, url, favicon);} both methods actually make the parameter view (WebView) loading the parameter url to avoid loading the url by the mobile browser. The first method is more commonly used. 2. However, in some cases, we may want to use WebView to open most of the links, while in some cases, we want to call a mobile browser to open them. This is the case in a recent project. This is actually very simple. We only need to modify the first method above. ClassMyWebViewClientextendsWebViewClient {@ brief (WebViewview, Stringurl) {// rewrite this method to indicate that clicking the link in the webpage is still redirected to the current webview, without skipping to the browser if (openWithWevView (url )) {view. loadUrl (url);} else {Uriuri = Uri. parse (url); // url startActivity (intent);} returntrue;} Where openWithWevView (url) is a self-written method, used to determine whether to use WevView to open the link.
How to return button events in ios webview to the app
Use of WebView for IOS Learning
1. Use UIWebView to load webpages
Run XCode 4.3 and create a new Single View Application named WebViewDemo.
2. Load WebView
Add the WebView member variable in ViewController. h and add the implementation in ViewController. m.
The network environment of the mobile phone changes in real time. When the network is slow, how can I prompt that the user's webpage is being opened? How can I prompt users when an error occurs when opening a webpage? At this time, we need to know when the webpage will be opened,
When the loading is complete and when an error occurs. We need to implement the <UIWebViewDelegate> protocol.
3. Modify the implementation protocol in ViewController. h as follows:
Press control + command + up key to switch to the ViewController. m file. Here we enter-(void) webView in the file to see the following implementation method:
Several important functions in UIWebView
1.-(void) webViewDidStartLoad :( UIWebView *) called when the webView page starts loading
2.-(void) webViewDidFinishLoad :( UIWebView *) called when the webView page is loaded
3.-(void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) Call when error webpage loading error
4. Add NSLog to implement these three methods.
Add
[WebView setDelegate: self]; sets the proxy. In this way, the preceding three methods can be called back.
The three methods are implemented as follows:
Run print:
15:20:29. 728 WebViewDemo [1001: f803] webViewDidStartLoad
15:20:29. 991 WebViewDemo [1001: f803] webViewDidFinishLoad
Let's try the error and turn off wifi and run the following command to print the result:
15:23:58. 939 WebViewDemo [1087: f803] webViewDidStartLoad
15:23:59. 016 WebViewDemo [1087: f803] webViewDidFinishLoad
The request results remain unchanged. Why is network disconnection still successful? Cache? I will try 163.com. This is the real result:
15:24:41. 131 WebViewDemo [1134: f803] webViewDidStartLoad
15:24:41. 149 WebViewDemo [1134: f803] didFailLoadWithError: Error Domain = NSURLErrorDomain Code =-1009 "The Internet connection appears to be offline. "UserInfo = 0x6b41660 {NSErrorFailingURLStringKey = www.163.com/, NSErrorFailingU ...... remaining full text>