UIWebView loading local HTML files

Source: Internet
Author: User

The reason for writing this article is that today is asked to "investigate the technology of loading local HTML in ios", so that a good memory is better than a bad one, and some things are recorded.

The scenario is this: The iOS app at hand has a portion of the UI that needs to be done with HTML, and the benefit is that this part of the UI can be more flexible, which means you can update this part of the UI (including styles, actions, and so on) without needing to upgrade the app. Now the very fire of the technology –facebook react native– is to solve this problem, after all, in the traditional development mode, updating the UI must upgrade the app, and upgrade the app is a very time-consuming process. Using the Web App segment instead of the native app has become a trend in mobile client development today. For the sake of brevity, and in order not to create a higher threshold for the project's successors, temporarily decided not to use react Native (for many reasons) and other well-known framework to go to this kind of thing, starting from 0 to explore. Say nonsense, perhaps after such a suffer, after learning react native will have more experience!

In short, the present idea is this:

    1. The app part UI is done by Html+js+css, and the HTML+JS+CSS code is published with the app;
    2. When the backend decides to update a part of the UI (HTML) of the app, the client downloads the latest HTML+JS+CSS package from the server, extracts the latest from the local, old Html+js+css resources, and renders the updated UI to the user.

So there are a few questions to consider:

    1. Load local HTML+JS+CSS resources (typically used in iOS, UIWebView);
    2. Unzip the compressed file downloaded from the server (html+js+ CSS package);
    3. The JS code and the OC code call each other;

This article mainly explores the first problem!

UIWebView Introduction

UIWebView is a very common control in iOS, a built-in browser control, and perhaps the most powerful and complex control. You can use it to browse the Web, open documents (such as PDF documents), and so on.

UIWebView can be used to display both local documents and network documents. Whether you are displaying a local file or a network file, this procedure can be collectively referred to as "loading." There are not many methods associated with "loading", only three:

//Loading Local content or Loading Contentfrom the Network-(void) Loadrequest: (nsurlrequest *) request; //string embedded in HTML structure -(void) loadhtmlstring: (nsstring *) stringBaseURL: (nsurl *) BaseURL; //Loading Local Content-(void) LoadData: (nsdata *) dataMIMEType: (NSString *) MIMETypetextencodingname: (nsstring *) textencodingnameBaseURL: (nsurl *) BaseURL;

There are not many points of knowledge associated with UIWebView (from some point of view, it can also be thought of as Apple encapsulation is better), in addition to the above three methods, it is necessary to understand the uiwebviewdelegate definition of some delegate methods:

//Start loading before calling -(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Requestnavigationtype: (uiwebviewnavigationtype) Navigationtype; //Start loading call -(void) Webviewdidstartload: (UIWebView *) WebView; ///load succeeded when called -(void) Webviewdidfinishload: (UIWebView *) WebView; //load failed when called -(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) error;

In addition, UIWebView seems to have what other need to understand the content, if necessary, add it later!

UIWebView loading local HTML files

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.