使用JSPatch平台热修复iosApp

news/2024/7/7 19:26:55

iOS产品开发中常常会遇到这种情况,线上发现一个严重bug,可能是一个crash,可能是一个功能无法使用,这时能做的只是赶紧修复Bug然后提交等待漫长的审核,即使申请加急也不会快到那里去,即使审核完了之后,还要盼望着用户快点升级,用户不升级还是在存在同样的漏洞,这样的情况让开发者付出了很大的成本才能完成Bug的修复。

JSPath就是为了解决这样的问题而出现的,只需要在项目中引入极小的JSPatch引擎,就可以还用JavaScript语言调用Objective-C的原生API,动态更新APP,修复BUG。

JSPaht本身是开源项目,项目地址:http://jspatch.com/,github地址: https://github.com/bang590/JSPatch
在他的官网上面给出了一个例子:

复制代码
 1 @implementation JPTableViewController
 2 ...
 3 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 4 {
 5   NSString *content = self.dataSource[[indexPath row]];  //可能会超出数组范围导致crash
 6   JPViewController *ctrl = [[JPViewController alloc] initWithContent:content];
 7   [self.navigationController pushViewController:ctrl];
 8 }
 9 ...
10 @end
复制代码

可以通过下发下面的JavaScript代码修复这个bug:

复制代码
 1 //JS
 2 defineClass("JPTableViewController", {
 3   //instance method definitions
 4   tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
 5     var row = indexPath.row()
 6     if (self.dataSource().length > row) {  //加上判断越界的逻辑
 7       var content = self.dataArr()[row];
 8       var ctrl = JPViewController.alloc().initWithContent(content);
 9       self.navigationController().pushViewController(ctrl);
10     }
11   }
12 }, {})
复制代码

JSPtch需要一个后台服务用来下发和管理脚本,并需要处理传输安全等。

注册获取AppKey

在平台上面注册一个账户,新建一个App可以拿到对应的AppKey。

导入SDK到项目中

SDK地址:http://jspatch.com/Index/sdk
当前下载下来的SDK版本名称是:JSPatch 2.framework,需要去掉中间的空格,不然导入项目的时候会报错。
导入项目的时候要选择Copy items if needed
还需要添加对于的依赖框架JavaScriptCore.frameworklibz.tbd.

添加JSPatch代码

在AppDelegate.m中添加代码:

复制代码
 1 #import "AppDelegate.h"
 2 #import <JSPatch/JSPatch.h>
 3 
 4 @implementation AppDelegate
 5 
 6 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 7     [JSPatch startWithAppKey:@"f78378d77e5783e8"];
 8     [JSPatch sync];
 9     return YES;
10 }
11 @end
复制代码

在平台中上传js修复文件

为了简单我们只上传一个简单的UIAlertView,弹出一个提示框:

复制代码
1 ar alertView = require('UIAlertView').alloc().init();
2 alertView.setTitle('Alert');
3 alertView.setMessage('AlertView from js');
4 alertView.addButtonWithTitle('OK');
5 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

id="iframe_0.1304364084538192" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://segmentfault.com/img/bVuOP8?_=5403182%22%20style=%22border:none;max-width:848px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.1304364084538192',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 800px; height: 141px;">

运行效果:
id="iframe_0.3852902944239738" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://segmentfault.com/img/bVuOQ0?_=5403182%22%20style=%22border:none;max-width:848px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3852902944239738',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 281px; height: 522px;">

 


http://www.niftyadmin.cn/n/592889.html

相关文章

Mac机安装Windows图解

<!--正文内容开始--><!-- google_ad_section_start --> <!-- 正文内部文字导航 : begin --><!-- 正文内部文字导航 : end -->作者&#xff1a;中关村在线 孙希萌 第1页&#xff1a;MacBook笔记本同样牵手WindowsMacBook笔记本同样牵手Windows 自从2005…

12C RAC for ASM添加磁盘步骤

RHEL 7.2使用EMC Powerpath扩容2T磁盘空间&#xff0c;需要添加至以用12C RAC for ASM系统中。下面是具体步骤&#xff0c;主机人员告知扩容别名为data_center_16、data_center_17 1&#xff1a;linux 7 系统下添加映射存储LUN(无需重启)1>查看机器HBA卡信息--两个节点机器都…

redis重启命令_跟我一起学Redis之Redis持久化必知必会

前言Redis是出了名的速度快&#xff0c;那是因为在内存中进行数据存储和操作&#xff1b;如果仅仅是在内存中进行数据存储&#xff0c;那就会导致以下问题&#xff1a;•数据随进程退出而消失&#xff1a;当服务器断电或Redis Server进程退出时&#xff0c;内存肯定随之释放&am…

android中自定义的dialog中的EditText无法弹出输入法解决方案

1.解决无法弹出输入法&#xff1a; 在show()方法调用之前&#xff0c;用dialog.setView(new EditText(context))添加一个空的EditText&#xff0c;由于是自定义的AlertDialog&#xff0c;有我们指定的布局&#xff0c;所以设置这个不会影响我们的功能&#xff0c;这样就可以弹出…

Android进阶之自定义ViewGroup—带你一步步轻松实现ViewPager

本文导语&#xff1a; ViewPager相信读者们都用得很多了&#xff0c;在项目中的使用场景可以说是相当的多了&#xff0c;例如&#xff1a; &#xff08;1&#xff09;项目框架的搭建&#xff0c;可以使用ViewPagerFragment &#xff08;2&#xff09;App引导页 &#xff08;3&a…

python获取windows系统信息_Python用WMI模块获取windowns系统信息

#!/usr/bin/env python#coding:utf-8import wmiimport osimport sysimport platformimport timedef sys_version():cwmi.WMI ()#获取操作系统版本for sys inc.Win32_OperatingSystem():print"Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s&qu…

andfix 增量升级更新 热补丁修复

from: http://blog.csdn.net/ityangjun/article/details/49097621 最近想着Android有没有什么热补丁更新技术的实现&#xff0c;于是上网搜索。接着实现了这一功能后&#xff0c;记录一下&#xff0c;免得自己忘了&#xff0c;可能文章会有出错的地方&#xff0c;望指出&#x…

python代码运行顺序_python 代码运行顺序问题?

Thread.join([timeout])Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.When the timeout a…