Uni APP launched the incremental update feature earlier this year. Of course, many app markets, especially Apple, are averse to incremental updates in order to prevent developers from providing illegal content to users without market approval. So for apps with incremental updates, the following points need to be noted:

1. Do not pop up incremental update prompt during shelf review

2. Incremental updates are downloaded using HTTPS to avoid being hijacked by third-party networks

3, do not update illegal content, do not destroy the benefits of the app market through incremental updates, such as iOS virtual payments to Apple commission, etc

What can you learn from this chapter?

1. How to implement the incremental update function 2. How to make the incremental update package 3

No more talking, get straight to the dry stuff!

How to implement incremental update functionality

This is not specific to UNI-app development, as all incremental updates should be (but the code uses Uni-app as an example).

1. On the APP side, call the server interface first to determine whether it needs to be updated

2. If you need to update, download the update package directly

3. Install the upgrade package and restart the app to complete the upgrade

Ok, let’s take uni-app as an example to see the code in action

The client implementation detects the upgrade in the onLaunch of the root directory app.vue with the following code:

// #ifdef APP-PLUS  
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {  
    uni.request({  
        url: 'http://www.javanx.cn/update/'.data: {  
            version: widgetInfo.version,  
            name: widgetInfo.name
        },  
        success: (result) = > {
            var data = result.data;
            if (data.update && data.wgtUrl) {  
                uni.downloadFile({  
                    url: data.wgtUrl,  
                    success: (downloadResult) = > {  
                        if (downloadResult.statusCode === 200) {  
                            plus.runtime.install(downloadResult.tempFilePath, {  
                                force: false  
                            }, function() {  
                                console.log('install success... ');  
                                plus.runtime.restart();  
                            }, function(e) {
                              // The error is very important. It is best to record the error in the server log for easy debugging or future maintenance
                              // How do I update to the server?
                              // Call an interface that returns e
                              console.error('install fail... '); }); }}}); }}}); });// #endif
Copy the code

#ifdef app-plus check if it needs to be updated

Plus.runtime. getProperty Obtains the application information corresponding to the specified APPID

plus.runtime.getProperty( plus.runtime.appid, function ( wgtinfo ) {
  / / appid properties
  var wgtStr = "appid:"+wgtinfo.appid;
  / / version attribute
  wgtStr += "<br/>version:"+wgtinfo.version;
  / / the name attribute
  wgtStr += "<br/>name:"+wgtinfo.name;
  / / the description attribute
  wgtStr += "<br/>description:"+wgtinfo.description;
  / / the author attributes
  wgtStr += "<br/>author:"+wgtinfo.author;
  / / email properties
  wgtStr += "<br/>email:"+wgtinfo.email;
  / / the features of attributes
  wgtStr += "<br/>features:"+wgtinfo.features;
  console.log( wgtStr ); });Copy the code

Uni. request invokes the server interface and passes in the current version. The server returns update whether the update package needs to be updated or wgtUrl when the update package needs to be updated.

DownloadFile Downloads the file resource to the local directory. The client directly initiates an HTTP GET request and returns the local temporary path tempFilePath for the file. If we need to monitor the download progress, we can do this:

var downloadTask = uni.downloadFile({
    url: 'https://www.javanx.cn/file/uni-app.rar'.complete: (a)= >{}}); downloadTask.onProgressUpdate(function(res) = >{
  console.log('Download Progress' + res.progress);
  console.log('Length of data downloaded' + res.totalBytesWritten);
  console.log('Total length of data expected to be downloaded' + res.totalBytesExpectedToWrite);
})
Copy the code

DownloadTask also provides the following methods: (1) abort the downloadTask

(2) onHeadersReceived ‘listens for HTTP Response Header events, which will be earlier than the request completion event, only supported by wechat applet platform

(3) offProgressUpdate cancels listening to download progress change events, which is only supported by wechat applet platform

(4) offHeadersReceived cancels listening on HTTP Response Header events, only supported by wechat applet platform

Plus.runtime. install(filePath, options, installSuccessCB, installErrorCB) The following types of installation packages are supported:

(1) Application resource installation package (WGT) with extension ‘.wgt’;

(2) Application resource Differential upgrade package (WGTU) with extension ‘.wgTU ‘;

(3) System program installation package (APK), which requires the installation package format supported by the current platform. Note: Only local addresses are supported. Before calling this method, you need to place the installation package from a network address or other location into a local directory accessible to the runtime environment.

The server implementation uses NodeJS as an example:

var express = require('express');  
var router = express.Router();  
var db = require('./db');  

// TODO queries configuration files or database information to see if there are any updates
function checkUpdate(params, callback) {  
    db.query('一段SQL'.function(error, result) {  
        // The update is not equal.
        var currentVersions = params.appVersion.split('. ');  
        var resultVersions = result.appVersion.split('. ');  

        if (currentVersions[0] < resultVersions[0]) {  
            // Major version updates are available
            callback({  
                update: true.wgtUrl: ' '.pkgUrl: result.pkgUrl // apK, ipA package can be downloaded address})}else if (currentVersions[currentVersions.length- 1] < resultVersions[resultVersions.length- 1]) {
          // Consider it a minor version update
            callback({  
                update: true.wgtUrl: result.wgtUrl, // WGT package downloadable address
                pkgUrl: ' '})}else {  
            // All other conditions are not updated
            callback({  
                update: false}}})); } router.get('/update/'.function(req, res) {  
    var appName = req.query.name;  
    var appVersion = req.query.version;  
    checkUpdate({  
        appName: appName,  
        appVersion: appVersion  
    }, function(error, result) {  
        if (error) {  
            throw error;  
        }  
        res.json(result);  
    });  
});
Copy the code

Ok, with the above functions, we will use HBuilderx to make the upgrade package. WGT, put it on the server, and use it together.

How does UNI-App make incremental updates

1. Update the version number in manifest.json. If the previous version is 1.0.0, the upgrade package can be 1.0.1, but otherwise, you can also see the server interface implementation, is determined by the version number.

2. Menu -> Release -> Native App- Make mobile App resource upgrade package

3. Wait for the console to generate the output location of the upgrade package

4, upload the upgrade package to the server, interface implementation and return: wgtUrl= just upgrade package

So now we have incremental updates in our app. Incremental updates can be implemented through the WGT package each time a small number of updates are made.

What do you need to know about Uni App incremental updates?

What should uni-APP incremental update be aware of

1. There are some adjustments in the SDK, such as the addition of Maps module, etc., which cannot be upgraded in this way, but must be upgraded in a whole package.

2. If the old non-custom component compilation mode does not have the NVUE file before, but the nvUE file is added in the update, this mode cannot be used. Since non-custom component compilation mode does not pack weeX engine without NVUE file, native engine cannot be added dynamically. The custom component mode includes the WEEX engine by default, regardless of nvUE files in the project.

3. The same cannot be done for native plug-ins.

4, #ifdef app-plus conditional compilation, only perform this upgrade logic on the APP platform.

5. Appid and version information are all information of HBuilder application during the development of HBuilderX real machine, so it is necessary to pack custom base or formal package to test the upgrade function.

Plus.runtime.version or uni.getSystemInfo() reads the apK/IPA package version number instead of the version information in the manifest.json resource. So plus.runtime.getProperty() is used to get the information.

7. After the WGT resource package is successfully installed, you must run plus.runtime.restart(). Otherwise, the new content does not take effect.

8. If the native engine of the App is not upgraded and only the WGT package is upgraded, pay attention to test the compatibility of WGT resources and the native base. By default, the platform alerts you to a version that does not match, and you can configure ignoring the prompts in the Manifest if the self-test is fine

conclusion

What did you learn today? Uni-app Incremental Update Have you learned?

Finally, thank you for your support.

The original address: www.javanx.cn/20190920/un…