UIWebView is a box that is used to load data from a Web page. UIWebView can be used to load PDFs, Word, doc, etc.
There are two ways to generate WebView, 1, storyboard by dragging 2, initializing by Alloc init.
Create WebView, _webview.datadetectortypes = Uidatadetectortypeall in the following text; is to identify the type in the webview, such as when there is a phone number in WebView, click on the number to call directly
-(UIWebView *) webView { if (! _webview) { = [[UIWebView alloc] initWithFrame:self.view.bounds]; = uidatadetectortypeall; } return _webview; }
Loading Web pages
//let the browser load the specified string and search using m.baidu.com- (void) LoadString: (NSString *) str {//1. URL location resource, address of resource requiredNSString *urlstr =str; if(! [Str hasprefix:@"/ http"]) {urlstr= [NSString stringWithFormat:@"http://m.baidu.com/s?word=%@", str]; } Nsurl*url =[Nsurl Urlwithstring:urlstr]; //2. Tell the URL to the server, request, request data from m.baidu.comNsurlrequest *request =[Nsurlrequest Requestwithurl:url]; //3. Send a request to the server[Self.webview loadrequest:request]; }
Loading HTML
// HTML is the design language of a Web page // <> Show Tags </> // scenario: Intercepting a portion of a Web page from appearing // For example: the full content of the page contains ads! After loading the completion page, delete the HTML from the ad section and then load // Used by a lot of news apps [Self.webview loadhtmlstring:@ "<p>Hello</p>" Baseurl:nil];
Loading local files
#pragma mark-load File -(void) loadFile { /// scenario: Load files downloaded from the server, For example PDF, or Word, picture and so on file nsurl *fileurl = [[NSBundle mainbundle] Urlforresource:@ " about. txt [ withextension:nil]; *request = [Nsurlrequest requestwithurl:fileurl]; [Self.webview loadrequest:request]; }
Load local files in a two-tier manner
#pragmaLoads the file as binary data-(void) Loaddatafile {//one of the most common cases//open IE, visit website, prompt you to install Flash plugin//If you do not have this application, it is not possible to open the corresponding file with UIWebView//Scenario: Load files downloaded from the server, such as PDF, or word, picture, etc.Nsurl *fileurl = [[NSBundle mainbundle] Urlforresource:@"IOS 7 Programming Cookbook.pdf"Withextension:nil]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:fileurl]; //The server 's response object, the server receives the request returned to the client.Nsurlresponse *respnose =Nil; NSData*data = [Nsurlconnection sendsynchronousrequest:request returningresponse:&Respnose Error:null]; NSLog (@"%@", Respnose. MIMEType); //in iOS development, if not a special requirement, all text encodings are UTF8//first explain the received binary data stream with UTF8[Self.webview loaddata:data Mimetype:respnose. MIMEType Textencodingname:@"UTF8"Baseurl:nil]; }
iOS WebView ways to load Web pages, files, and HTML