Use of WebView in IOS, use of IOSWebView

Source: Internet
Author: User

Use of WebView in IOS, use of IOSWebView

UIWebView is the most common control in iOS sdk. Is a built-in browser control, which can be used to browse webpages, open documents, etc. UIWebView can load html/htm, pdf, docx, txt and other files.
The Safari browser that comes with the system is implemented through UIWebView. Next I will work out how to use webView.

1. Build the UI

 

  The full name of 2MIME is "Multipurpose Internet Mail Extensions"

Multi-purpose Internet Mail Extension is an internet standard. It was first applied to the email system and later to the browser. here we need to get the MIMIE type of the file. How can we get it? Obtain

-(NSString *) getMimeType :( NSString *) path {// local file path NSURL * url = [NSURL fileURLWithPath: path]; // create a request NSURLRequest * request = [NSURLRequest requestWithURL: url]; NSURLResponse * response; // send a synchronous request to obtain the response type [NSURLConnection sendSynchronousRequest: request returningResponse :( & response) error: nil]; return response. MIMEType ;}

3. Load PDF files

// Monitor all data types: Set the phone number, website address, email, and date to the link text [webView setDataDetectorTypes: UIDataDetectorTypeAll]; // set the file path to be displayed, and obtain the mimeType type NSString * str = [[NSBundle mainBundle] pathForResource: @ "iOS6Cookbook.pdf" ofType: nil]; NSString * mimeType = [self getMimeType: str]; // call the method to load data NSData * data = [NSData dataWithContentsOfFile: str]; [self. webView loadData: data MIMEType: mimeType textEncodingName: @ "UTF-8" baseURL: nil];

:

4. Load the txt file.

5. load HTML. Now there is an html file

Load the page, obtain the string, and then display it:

// Monitor all data types: Set the phone number, website address, email, and date to the link text [webView setDataDetectorTypes: UIDataDetectorTypeAll]; // set the file path to be displayed, and obtain the mimeType type NSString * str = [[NSBundle mainBundle] pathForResource: @ "book_v2.html" ofType: nil]; NSString * mimeType = [self getMimeType: str]; // call the method to load data NSData * data = [NSData dataWithContentsOfFile: str]; NSString * htmlStr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; [self. webView loadHTMLString: htmlStr baseURL: nil];

:

6. Load URL requests

// Monitor all data types: Set the phone number, url, email, and date to the link text [webView setDataDetectorTypes: UIDataDetectorTypeAll]; NSURL * url = [NSURL URLWithString: @ "http://21-sun.com"]; [self. webView loadRequest: [NSURLRequest requestWithURL: url];

:

7. Proxy Method

// Call-(void) webViewDidStartLoad (UIWebView *) webView/call-(void) webViewDidFinishLoad (UIWebView *) when the webpage is loaded *) webView // call when a webpage loading error occurs-(void) webView :( UIWebView) webView didFailLoadWithError :( NSError) error // This method is triggered for every request on the webpage, if NO is returned, this request is not executed (usually used for communication between JS and iOS)-(BOOL) webView :( UIWebView) webView shouldStartLoadWithRequest :( NSURLRequest) request navigationType :( UIWebViewNavigationType) navigationType

 

For more information, clickView Source CodeRun the test in person.

For questions or technical exchanges, please join the official QQ group: (452379712)

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.