IOS App经常会遇到这种状况,线上发现一个严重bug
,多是某一个地方Crash
,也多是一个功能没法使用,这时能作的只有赶忙修复Bug而后提交app store
等待漫长的审核。
即便申请加急审核可是审核速度仍然不会快到那里去,即便审核完了以后,还要盼望着用户快点升级,用户不升级一样的漏洞一直存在,这种状况让开发者付出了很大的成本才能完成对于Bug
的修复,有可能还须要出现强制升级的状况。ios
这样状况如今有办法改善,JSPatch
就是为了解决这样的问题而出现的,只须要在项目中引入极小的一个JSPatch
引擎,就可使用JavaScript
语言调用Objective-C
的原生API,动态更新App,修复BUG。git
JSPatch
是一个开源的项目,项目网站:http://jspatch.com/,Github地址: https://github.com/bang590/JSPatch
在JSPatch
的官网上面给出了一个例子:github
@implementation JPTableViewController ... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *content = self.dataSource[[indexPath row]]; //可能会超出数组范围致使crash JPViewController *ctrl = [[JPViewController alloc] initWithContent:content]; [self.navigationController pushViewController:ctrl]; } ... @end
这里会出现一个数组越界的Crash
能够经过下发下面的JavaScript代码修复这个Bug:数组
//JS defineClass("JPTableViewController", { //instance method definitions tableView_didSelectRowAtIndexPath: function(tableView, indexPath) { var row = indexPath.row() if (self.dataSource().length > row) { //加上判断越界的逻辑 var content = self.dataArr()[row]; var ctrl = JPViewController.alloc().initWithContent(content); self.navigationController().pushViewController(ctrl); } } }, {})
JSPtch
须要一个后台服务用来下发和管理脚本,并须要处理传输安全等JSPatch
平台提供了对应的服务。安全
在JSPatch
平台上面注册一个帐户,新建一个App就能够拿到对应的AppKey。服务器
SDK地址:http://jspatch.com/Index/sdk
当前下载下的SDK版本名称是:JSPatch 2.framework
,须要去掉中间的空格,否则导入项目的时候会报错。
导入项目的时候要选择Copy items if needed
。
还须要添加对于的依赖框架JavaScriptCore.framework
和libz.tbd
.app
在AppDelegate.m
中添加代码:框架
#import "AppDelegate.h" #import <JSPatch/JSPatch.h> @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [JSPatch startWithAppKey:@"f78378d77e5783e8"]; [JSPatch sync]; return YES; } @end
JavaScript
修复文件为了简单咱们只上传一个简单的UIAlertView
,弹出一个提示框:jsp
ar alertView = require('UIAlertView').alloc().init(); alertView.setTitle('Alert'); alertView.setMessage('AlertView from js'); alertView.addButtonWithTitle('OK'); alertView.show();
这段代码用JavaScript
实例化了UIAlertView
,文件名须要命名为main.js
。网站
把main.js
上传到服务器上,下发到版本为1.0的客户端上面。
在请求服务加载脚本的时候出现了一个错误:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
这个错误出现的缘由是ios9引入了新特性App Transport Security(ATS)
,简单来讲就是App内部的请求必须使用HTTPS协议
。
很明显这里的url并无使用https,咱们能够经过设置先规避掉这个问题:
1. 在info.plist中添加NSAppTransportSecurity类型为Dictionary. 2. 在NSAppTransportSecurity中添加NSAllowsArbitraryLoads类型为Boolean,值为YES
运行效果以下:
这样就能够直接修复掉线上bug了,不须要等待App Store
的审核。