├── LICENSE
├── README.md
├── plugin.xml
├── src
├── android
│ ├── UpdateApp.java
│ └── softupdate_progress.xml
└── ios
│ ├── CDVUpdateApp.h
│ └── CDVUpdateApp.m
└── www
└── updateAppPlugin.js
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Robin Lei
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | cordova-plugin-updateapp
2 | =========
3 |
4 | 基于cordova插件形式开发的版本升级插件,支持android和iOS。
5 |
6 | 说明:
7 | ========
8 | + 该版本插件android和iOS均是基于服务器上的版本信息文件
9 | + iOS的更新是基于企业证书签名,内网分发的应用编写的
10 | + iOS下需要基于appstore的lookup接口做检查更新的话,这个插件不适用,需要稍微做些修改
11 | + 2015年3月更新,提交到appstore的应用如果有检查更新功能,会被拒绝
12 |
13 | 安装:
14 | ========
15 | 1. `cordova plugin add https://github.com/south-pacific/cordova-plugin-updateapp.git`
16 | 2. `cordova build android` 或者 `cordova build ios`
17 | 3. 在服务器上放置版本信息文件`androidVersion.json` 或者 `iosVersion.json`
18 |
19 | 使用:
20 | ========
21 | ```javascript
22 | // 获取APP当前版本号
23 | window.plugins.updateApp.getCurrentVerInfo(function (currentVersionCode) {
24 | console.log(currentVersionCode);
25 | });
26 |
27 | // 获取服务器上APP的版本号,versionServer为版本信息文件地址
28 | window.plugins.updateApp.getServerVerInfo(function (serverVersionCode) {
29 | console.log(serverVersionCode);
30 | }, function () {
31 | console.log("出现异常");
32 | }, versionServer);
33 |
34 | // 检查并更新,versionServer为版本信息文件地址
35 | window.plugins.updateApp.checkAndUpdate(versionServer);
36 |
37 | // 获取APP当前版本号
38 | // 与getCurrentVerInfo方法不同之处在于android下getCurrentVerInfo返回的是versionCode
39 | // 该方法返回的是versionName
40 | window.plugins.updateApp.getAppVersion(function (version) {
41 | console.log(version);
42 | });
43 | ```
44 |
45 | androidVersion.json:
46 | =========
47 | `[{"verCode":"最新版apk的versionCode","verName":"最新版apk的versionName","apkPath":"apk的地址"}]`
48 |
49 | iosVersion.json:
50 | =========
51 | `{"verName":"最新版ipa的版本号","ipaPath":"plist的地址或者app的itunes地址"}`
52 |
--------------------------------------------------------------------------------
/plugin.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 | UpdateApp
8 |
9 | 应用内提示更新APP
10 |
11 | MIT
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | 已经是最新版本
33 | 软件更新
34 | 检测到新版本,立即更新吗?
35 | 更新
36 | 稍后更新
37 | 正在更新
38 | 取消
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/src/android/UpdateApp.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/south-pacific/cordova-plugin-updateapp/5951c71d507368b564521615918834a5badda81d/src/android/UpdateApp.java
--------------------------------------------------------------------------------
/src/android/softupdate_progress.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
11 |
12 |
--------------------------------------------------------------------------------
/src/ios/CDVUpdateApp.h:
--------------------------------------------------------------------------------
1 | #import
2 |
3 | @interface CDVUpdateApp : CDVPlugin
4 |
5 | - (void)getCurrentVersion:(CDVInvokedUrlCommand*)command;
6 | - (void)getServerVersion:(CDVInvokedUrlCommand*)command;
7 | - (void)checkAndUpdate:(CDVInvokedUrlCommand*)command;
8 | - (void)getVersionName:(CDVInvokedUrlCommand*)command;
9 |
10 | @end
--------------------------------------------------------------------------------
/src/ios/CDVUpdateApp.m:
--------------------------------------------------------------------------------
1 | #import "CDVUpdateApp.h"
2 | #import
3 |
4 | @implementation CDVUpdateApp
5 |
6 | NSString *ipaPath;
7 |
8 | - (void)getCurrentVersion:(CDVInvokedUrlCommand*)command
9 | {
10 | [self.commandDelegate runInBackground:^{
11 | NSString* version = [self getCurrentVersionCode];
12 |
13 | CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:version];
14 |
15 | [self.commandDelegate evalJs:[pluginResult toSuccessCallbackString:command.callbackId]];
16 | }];
17 | }
18 |
19 | - (void)getVersionName:(CDVInvokedUrlCommand*)command
20 | {
21 | [self.commandDelegate runInBackground:^{
22 | NSString* version = [self getCurrentVersionName];
23 |
24 | CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:version];
25 |
26 | [self.commandDelegate evalJs:[pluginResult toSuccessCallbackString:command.callbackId]];
27 | }];
28 | }
29 |
30 | - (void)getServerVersion:(CDVInvokedUrlCommand*)command
31 | {
32 |
33 | NSString *URL = [command argumentAtIndex:0];
34 |
35 | [self.commandDelegate runInBackground:^{
36 | NSDictionary *resultDic = [self getServerVersionCode:URL];
37 |
38 | if (resultDic == nil) {
39 | CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"error"];
40 |
41 | [self.commandDelegate evalJs:[pluginResult toErrorCallbackString:command.callbackId]];
42 | } else {
43 | NSString *lastVersion = [resultDic objectForKey:@"verName"];
44 |
45 | CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:lastVersion];
46 |
47 | [self.commandDelegate evalJs:[pluginResult toSuccessCallbackString:command.callbackId]];
48 | }
49 | }];
50 | }
51 |
52 | - (void)checkAndUpdate:(CDVInvokedUrlCommand*)command
53 | {
54 | NSString *URL = [command argumentAtIndex:0];
55 |
56 | [self.commandDelegate runInBackground:^{
57 | NSDictionary *resultDic = [self getServerVersionCode:URL];
58 |
59 | if (resultDic) {
60 | NSString *lastVersion = [resultDic objectForKey:@"verName"];
61 | ipaPath = [resultDic objectForKey:@"ipaPath"];
62 |
63 | if ([lastVersion compare:[self getCurrentVersionCode] options:NSNumericSearch] == NSOrderedDescending) {
64 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
65 | alert.tag = 10000;
66 | dispatch_async(dispatch_get_main_queue(), ^{
67 | [alert show];
68 | });
69 | }
70 | }
71 | }];
72 | }
73 |
74 | - (NSString *)getCurrentVersionCode {
75 | return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
76 | }
77 |
78 | - (NSString *)getCurrentVersionName {
79 | NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
80 | if (version == nil) {
81 | version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
82 | }
83 | return version;
84 | }
85 |
86 | - (NSDictionary *)getServerVersionCode:(NSString *)url {
87 |
88 | @try {
89 | NSError *error = nil;
90 |
91 | //加载一个NSURL对象
92 | NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0f];
93 | //将请求的url数据放到NSData对象中
94 | NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
95 | //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
96 | NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
97 |
98 | return resultDic;
99 | }
100 | @catch (NSException *exception) {
101 | return nil;
102 | }
103 | }
104 |
105 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
106 | {
107 | if (alertView.tag == 10000) {
108 | if (buttonIndex == 1) {
109 | NSURL *url = [NSURL URLWithString:ipaPath];
110 | [[UIApplication sharedApplication]openURL:url];
111 | }
112 | }
113 | }
114 |
115 |
116 | @end
--------------------------------------------------------------------------------
/www/updateAppPlugin.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 检查并更新APP
3 | * @author robin
4 | */
5 | function UpdateApp() {}
6 | UpdateApp.prototype.checkAndUpdate = function(checkPath) {
7 | cordova.exec(null, null, "UpdateApp", "checkAndUpdate", [checkPath]);
8 | }
9 | UpdateApp.prototype.getCurrentVerInfo = function(successCallback) {
10 | cordova.exec(successCallback, null, "UpdateApp", "getCurrentVersion", []);
11 | }
12 | UpdateApp.prototype.getServerVerInfo = function(successCallback, failureCallback, checkPath) {
13 | cordova.exec(successCallback, failureCallback, "UpdateApp", "getServerVersion", [checkPath]);
14 | }
15 | UpdateApp.prototype.getAppVersion = function(successCallback, failureCallback) {
16 | cordova.exec(successCallback, failureCallback, "UpdateApp", "getVersionName", []);
17 | }
18 | cordova.addConstructor(function() {
19 | if (!window.plugins) {
20 | window.plugins = {};
21 | }
22 | window.plugins.updateApp = new UpdateApp();
23 | });
--------------------------------------------------------------------------------