Use of WebView in iOS

Source: Internet
Author: User

UIWebView is one of the most commonly used controls in the iOS SDK. is a built-in browser control, we can use it to browse the Web, open the document and so on, UIWebView be able to load html/htm, PDF, docx, TXT and other formats of the file
The system comes with a safari browser that is implemented through UIWebView. I'm going to open it up next. How to use WebView easily.

1. Build UI Interface

  The English name of 2MIME is "multipurpose Internet Mail Extensions"

Multi-purpose Internet Mail extension, is an Internet standard, the earliest application in the e-mail system, later applied to the browser, where we need to get the file Mimie type, how to get it? We get it by the following way

-(NSString *) GetMimeType: (NSString *) path{    //local file path    Nsurl * Url=[nsurl Fileurlwithpath:path];    Create Request    Nsurlrequest * request=[nsurlrequest Requestwithurl:url];    Nsurlresponse * response;     Send sync request Get response type    [Nsurlconnection sendsynchronousrequest:request returningresponse: (&response) error:nil];    Return  response. MIMEType;}

3 Loading PDF files

    Monitor all data types: Set phone numbers, URLs, emails, and dates into linked text    [WebView Setdatadetectortypes:uidatadetectortypeall];        Set the file path that needs to be shown, and get the mimetype type    NSString * str=[[nsbundle Mainbundle] pathforresource:@ "Ios6cookbook.pdf" OfType: NIL];    NSString * mimetype=[self GETMIMETYPE:STR];        Call method to load data    NSData * Data=[nsdata datawithcontentsoffile:str];[ Self.webview loaddata:data mimetype:mimetype textencodingname:@ "UTF-8" baseurl:nil];

:

4, loading txt and the above similar this article does not do the explanation  

5, loading HTML, now has an HTML file

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

Monitor all data types: Set phone numbers, URLs, emails, and dates into linked text    [WebView Setdatadetectortypes:uidatadetectortypeall];    Set the file path that needs to be shown, and get the mimetype type    NSString * str=[[nsbundle Mainbundle] pathforresource:@ "book_v2.html" oftype:nil];    NSString * mimetype=[self GETMIMETYPE:STR];    Call method Load Data    NSData * data=[nsdata datawithcontentsoffile:str];       NSString * htmlstr=[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];    [Self.webview loadhtmlstring:htmlstr Baseurl:nil];

:

6. Load URL Request

    Monitor all data types: Set phone numbers, URLs, emails, and dates into linked text    [WebView Setdatadetectortypes:uidatadetectortypeall];     Nsurl * Url=[nsurl urlwithstring:@ "http://21-sun.com"];    [Self.webview loadrequest:[nsurlrequest Requestwithurl:url];

:

7. Agent method

The page starts to load when called-(void) Webviewdidstartload: (UIWebView *) webview//when the page load is complete call-(void) Webviewdidfinishload: (UIWebView *) webview//page load Error call-(void) WebView: (UIWebView) WebView didfailloadwitherror: (nserror) error//every request in the Web page is triggered by this method , return no means do not execute this request (commonly used in communication between JS and iOS)-(BOOL) WebView: (UIWebView) WebView shouldstartloadwithrequest: (nsurlrequest) Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype

To learn more about the small partners, you can click to view the source code , run the test yourself.

Inquiries or technical exchanges, please join the official QQ Group: (452379712)

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Use of WebView in iOS

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.