How to implement offline caching strategy for a hybrid app? In simple terms, your app is just a shell, and the actual content loaded inside is H5. How to optimize the speed of loading content? First, let’s take a look at NSURLProtocol It looks like a protocol, but it is actually a class that inherits from NSObject. Its function is to handle the loading of specific URL protocols. It is an abstract class that provides the infrastructure for handling URLs using characteristic URL schemes. You can create a subclass of NSURLProtocol to support custom protocols or URL schemes in your application. Applications never need to instantiate an NSURLProtocol subclass directly. When a download begins, the system creates an appropriate protocol object to respond to the URL request. All you have to do is define your own protocol and then call registerClass: when your app starts to let the system know about your protocol. Note: You cannot customize URL schemes and protocols in watchOS 2 and later. To support specific custom requests, you can first define NSURLRequest or NSMutableURLRequest. To make these custom objects implement the request, you need to use NSURLProtocol's propertyForKey:inRequest: and setProperty:forKey:inRequest, and then you can customize the NSURLResponse class to simulate the return information. Next, we will start offline caching of UIWebView. UIWebView offline cache processing First, we need to customize a subclass of NSURLProtocol and in AppDelegate.m
Register. All the following operations are performed in our custom ZGURLProtocol. Let's first look at the role of registerClass: Try registering a subclass of NSURLProtocol to make it visible to the URL Loading System. The URL Loading System is a set of classes and protocols that allow your application to access content generated by URLs, such as requests, received content, and caches. When the URL Loading System starts loading a request, each registered protocol class is called in turn to determine whether it can be initialized with the specified request. The first method called is:
Perform cache filtering in this method. For example, if you want to cache only js, then determine the suffix of the request path. If it is js, return YES, otherwise return NO. If YES is returned, it means that the request is handled by the custom URLProtocol. There is no guarantee that all registered NSURLProtocols can be handled. If you define multiple NSProtocol subclasses, these subclasses will be called in reverse order. That is to say, if you write:
Then the first thing executed is ZProtocol. If the initWithRequest: parameter returns YES, the request is processed by ZProtocol and does not go through ZGURLProtocol. If the initWithRequest: of ZProtocol returns NO, the request continues to be passed down and processed by other NSURLProtocol subclasses. Once YES is returned, the request will be handled by your own subclass, which will first call:
This is an abstract method that subclasses must implement. Normally, we return the request directly, but you can also modify the request directly, including headers, hosts, etc. You can redirect the specified request. Here, we just return the existing request. Then, the request will begin:
The function of this method is to start requesting the request specified by the protocol. This method is also a method that the protocol subclass must implement. The operations performed here are: First determine whether there is cached data. If so, create an NSURLResponse yourself, then put the cached data in, perform some client operations, and then return; if there is no cached data, create a new NSURLConnection and then send the request. Let's first talk about the case where there is a cache:
(model is cached data) If there is a cache, directly use the cached data and MIME type, then build NSURLResponse, and then call the proxy method through the protocol client. The client here is a protocol, as follows:
This protocol provides an interface for NSURLProtocol subclasses to communicate with the URL Loading System. An app must not implement this protocol. If there is a cache, the callback method is called and then processed. Without caching: Instantiate a connection and then initiate a request. When we receive the response:
Next comes receiving data:
After receiving the data, it calls:
This method ends the call after the home, and we need to cache the requested data here. In this way, we have the return data of the specified URL locally. There is one important thing that has not been introduced here, that is
Here
The function is to set the value associated with a specific key in the specified request. Prevent multiple calls to a request. In this way, we have completed the offline cache of UIWebView. Here I encapsulate a ZGUIWebViewCache. If you are interested, you can take a look. Offline cache processing of WKWebView WKWebView offline cache is similar to UIWebView cache, except that after calling NSURLProtocol's canInitWithRequest: method at the beginning, subsequent requests seem to have nothing to do with NSURLProtocol. It is said on the Internet that WKWebView's requests are in an independent process, so NSURLProtocol is not used. Here, it is processed by NSURLProtocol+WKWebView class. For details, see: ZGWKWebViewCache . The rest of the process is similar to UIWebView cache processing. The above is the implementation of offline caching of web pages. |
<<: Android skinning principle and Android-Skin-Loader framework analysis
With the rapid development of the mobile Internet...
Mammoths are animals that can adapt to cold clima...
Dragon Raising its Head refers to the phenomenon ...
2016 is the explosive year for information flow a...
Only a week later, Apple released the first devel...
A popular online event can not only directly brin...
Apple, which has lowered its prices every month t...
You must have heard of giant panda blood - Rh neg...
Nowadays, due to the ever-faster pace of life, pe...
1. Case Background As a rule, before talking abou...
The basic tone of the APP operation and promotion...
Short videos are really popular. Once upon a time...
Recently, Urban Sky, a U.S. stratospheric balloon...
Author | Yang Yang, vivo Internet Platform Produc...
When it comes to competitor research, I think mos...