The webview Method for iOS to load webpages, files, and html,
UIWebView is a box used to load webpage data. UIWebView can be used to load pdf, word, doc, and other files
There are two ways to generate webview: 1. Drag and Drop through storyboard 2. initialize webview through alloc init
Create a webview. In the following text, _ webView. dataDetectorTypes = UIDataDetectorTypeAll is used to identify the type in webview. For example, if a webview has a phone number, you can click the number to make a direct call.
- (UIWebView *)webView { if (!_webView) { _webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; _webView.dataDetectorTypes = UIDataDetectorTypeAll; } return _webView; }
Load webpage
// Let the browser load the specified string and use m.baidu.com for search-(void) loadString :( NSString *) str {// 1. URL: locate the resource. The resource address NSString * urlStr = str; if (! [Str hasPrefix: @ "http: //"]) {urlStr = [NSString stringWithFormat: @ "http://m.baidu.com/s? Word = % @ ", str];} NSURL * url = [NSURL URLWithString: urlStr]; // 2. send the URL to the server. For the request, request data from m.baidu.com NSURLRequest * request = [NSURLRequest requestWithURL: url]; // 3. send a request to the server [self. webView loadRequest: request];}
Load html
// HTML is the design language of the webpage. // <> MARK </> // Use Case: capture a part of the webpage. // For example, the complete content of the webpage contains an advertisement! After the page is loaded, delete the HTML of the AD part, and then load // It. webView loadHTMLString: @ "<p> Hello </p>" baseURL: nil];
Load local files
# Pragma mark-Load file-(void) loadFile {// Application Scenario: load files downloaded from the server, such as pdf or word, NSURL * fileURL = [[NSBundle mainBundle] URLForResource: @ "about .txt" withExtension: nil]; NSURLRequest * request = [NSURLRequest requestWithURL: fileURL]; [self. webView loadRequest: request];}
Load local files in level-2 mode
# Pragma loads a file in the form of binary data-(void) loadDataFile {// the most common situation // open IE and visit the website, you are prompted to install the Flash plug-in // if you do not have this application, you cannot use UIWebView to open the corresponding file. // Application Scenario: load files downloaded from the server, such as pdf, or NSURL * fileURL = [[NSBundle mainBundle] URLForResource: @ "iOS 7 Programming cookbooksion" withExtension: nil]; NSURLRequest * request = [NSURLRequest requestWithURL: fileURL]; // response object of the server. The server receives the NSURLResponse * respnose = nil; NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & respnose error: NULL]; NSLog (@ "% @", respnose. MIMEType); // in iOS development, if it is not a special requirement, all the text encoding will use UTF8 // use UTF8 to explain the received binary data stream [self. webView loadData: data MIMEType: respnose. MIMEType textEncodingName: @ "UTF8" baseURL: nil];}