iOS development-uiwebview loading local and network data

Source: Internet
Author: User

UIWebView is a built-in browser control, you can use it to browse the Web, open the document, about browsing the web example can refer to UC, mobile browser, as for the document browsing the mobile phone many book reading software, UIWebView is a hybrid, the specific features of the control built-in, To implement some basic functions. UIWebView can view HTML pages, PDF files, docx files, txt file files, the system comes with Safari is UIWebView implementation.

Basic Layout

The layout of the page is simple as a text box, a button, a uiwebview, and a page layout as follows:

If you want to be simple, you can actually use UIWebView, but you need to prepare some text data, as follows:

Data Loading

① directly stitching HTML, with UIWebView display, viewdidload Add code:

    Load HTML string directly    NSString *[email protected] "

 

② loading the local HTML page, book.html the code:

<! DOCTYPE html>

Viewdidload Code:

    NSString *filepath = [[NSBundle mainbundle]pathforresource:@ "book" oftype:@ "html"];    NSString *htmlstring = [NSString stringwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil];    [Self.webview loadhtmlstring:htmlstring Baseurl:[nsurl Urlwithstring:filepath];

③ loading the local PDF file, Viewdidload code:

    Nsurl *url = [[NSBundle mainbundle]urlforresource:@ "Book.pdf" withextension:nil];        Nsurlrequest *request = [Nsurlrequest requestwithurl:url];        [Self.webview Loadrequest:request];

  

The second way to load a PDF:

    NSString *path = [[NSBundle mainbundle]pathforresource:@ "Book.pdf" oftype:nil];        Loads the data in binary form    nsdata *data = [NSData Datawithcontentsoffile:path];        [Self.webview loaddata:data mimetype:@ "application/pdf" textencodingname:@ "UTF-8" baseurl:nil];

④ loading the local TXT file, the Viewdidload code is as follows:

Load txt        nsurl *url = [[NSBundle mainbundle]urlforresource:@ "Book.txt" withextension:nil];        Set URL        [Self.webview loadrequest:[nsurlrequest Requestwithurl:url];

  

⑤ load the Word,viewdidload code as follows:

    Load Word    nsurl *url = [[NSBundle mainbundle]urlforresource:@ "Book.docx" withextension:nil];    Set load URL    [Self.webview loadrequest:[nsurlrequest Requestwithurl:url];

  

⑥ load the network data, the Jump button event is implemented as follows:

    Nsurlrequest *request =[nsurlrequest Requestwithurl:[nsurl URLWithString:self.urlText.text]];    [Self.webview Loadrequest:request];

  

⑦ set up delegates to process data at different stages, Implement Uiwebviewdelegate, set itself as the delegate object;

  [Self.webview setdelegate:self];

Three common methods:

Load Start-(void) Webviewdidstartload: (UIWebView *) webview{    NSLog (@ "method call at the beginning of Load");} Load complete-(void) Webviewdidfinishload: (UIWebView *) webview{    NSLog (@ "Computer method call when loading is complete");} Load Error-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (Nserror *) error{    NSLog (@ "Call to load Error");}

  

iOS development-uiwebview loading local and network data

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.