Uiwebview allows you to create a web browser, similar to Safari, instead of starting safsri in the program. Do you think it's great? Let's talk nonsense and start with the topic.
1. Create a uiwebview
CGRect bouds = [[UIScreen manScreen]applicationFrame];UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
Ii. Set attributes
Webview. scalespagetofit = yes; // automatically scales the page to adapt to webview. detectsphonenumbers = yes; // automatically detects the phone number on the page, and click
3. Display webpage view uiwebview
[self.view addSubview:webView];
4. Load content
Nsurl * url = [nsurl urlwithstring: @ "http://www.youku.com"]; // create urlnsurlrequest * request = [nsurlrequest requestwithurl: url]; // create nsurlrequest [webview loadrequest: request]; // Load
You can also load a local resource:
Nsurl * url = [nsurl fileurlwithpath: filepath]; // create urlnsurlrequest * request = [nsurlrequest requestwithurl: url]; // create nsurlrequest [webview loadrequest: request]; // Load
Uiwebview also supports loading an nsstring object as the source. You can provide a basic URL to guide the uiwebview object to follow the link and load remote resources:
[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];
5. Navigation
The uiwebview class manages browser navigation actions. You can use the goforward and Goback methods to control the forward and backward actions:
[Webview Goback]; [webview goforward]; [webview reload]; // overload [webview stoploading]; // cancel loading content
Vi. uiwebviewdelegate delegate proxy
Uiwebview supports a set of delegate methods, which are notified at a specific time. To use these methods, you must first set the webview delegate:
webView.delegate = self;
The first parameter of each delegate method below is a pointer to a uiwebview. Therefore, you can use a delegate to multiple webpage views.
-(Bool) webview :( uiwebview *) webview shouldstartloadwithrequest :( nsurlrequest *) reuqest navigationtype :( uiwebviewnavigationtype) navigationtype; // when the webpage view is instructed to load the content, it is notified. Yes should be returned for loading. You can obtain the reason for the request initiation through the navigation type parameter, which can be any of the following values: Repeated reloaduiwebviewnavigationtypeformresubmitteduiwebviewnavigationtypeother
-(Void) webviewdidstartload :( uiwebview *) webview; // you will be notified when the webpage view has started to load a request.
-(Void) webviewdidfinishload :( uiwebview *) webview; // you will be notified after the webpage view ends loading a request.
-(Void) webview :( uiwebview *) webview didfailloadwitherror :( nserror *) error; // get a notification when an error occurs during request loading. An nsserror object is provided to identify the error type.
So far, I have finished all the work. Do you think it is very useful? Are you trying to write a demo?