使用就不多写了,网上大佬贼多。
因为想做一个强制更新,不更新就退出的常见功能,就给cordova-hcp.json加了个字段mustUpdateHtml
{
"name": "n3app",
"autogenerated": true,
"content_url": "your_www",
"update": "resume",
"min_native_interface": 1,
"mustUpdateHtml": "true"
}
这个true是个字符串…原因在下面。
使用chcp.fetchUpdate,这个data.config会包含服务器端chcp.json的内容!
在安卓表现:返回完整的json字符串(json文件有啥都返回),前端记得parse一下;
在ios表现:插件只返回五个字段:release,update,min-native-interface,content_url,ios_identifier,而且返回的是一个对象,不需要parse!如果自定义字段需要在ios配置文件修改。
var mustUpdateHtml =(device.platform == 'Android'? JSON.parse(data.config).mustUpdateHtml: data.config.mustUpdateHtml) == 'true'
需要修改的文件:
\plugins\cordova-hot-code-push-plugin\src\ios\Config\HCPContentConfig.m
如果打包还是生成过,平台的iOS里面会有个以app名字作为文件名的文件夹,里面的文件也得改改,上下两个文件内容其实一样的(我也不知道啥操作,反正全局搜索,然后改的…)
\platforms\ios\app_name\Plugins\cordova-hot-code-push-plugin\Config\HCPContentConfig.m
iOS增加string字段:mustUpdateHtml
15:@property (nonatomic, strong, readwrite) NSString *mustUpdateHtml;
25:static NSString *const test = @"mustUpdateHtml";
110:if (_mustUpdateHtml) {
jsonObject[mustUpdateHtml] = _mustUpdateHtml;
}
125:contentConfig.mustUpdateHtml = jsonObject[mustUpdateHtml];
本菜不会这个ios开发语言,所以!只能复制他用过的string!所以开始的那个json里面是"true"!
上述文件其实只有四个字段,没有ios_identifier,作者针对这个好像专门有个文件操作-,-
※遗留问题※:即使我在config.xml里面关闭了自动下载和安装,这个fetchUpdate还是会获取到所有更新文件!目前项目还小,再做一下固定资源的分离打包,看不出来差别。听说这个插件已经被放弃维护了…
再分享一个插件用于ios退出应用:
https://github.com/crossee/cordova-plugin-voca-exit
这个插件覆盖了navigator.app.exitapp()方法,定义在plugin.xml的clobbers,会导致安卓端关闭失败,引入前记得修改,下面是我的修改调用。
<clobbers target="iosAppExit" />
if (device.platform == 'iOS') {
iosAppExit.exitApp()
} else {
navigator.app.exitApp()
}