iOS 快速集成热修复(JSPatch)

前言

项目上线以后, 后台改变了数据结构, 导致某些功能使用不了, 直接闪退, 后台越来越调皮了

正题

JSPatch 的优点

非侵入式
上手快
相关服务成熟
使用简单

1:上代码

第一步:创建Demo, 在ViewController里面添加一个Label, 声明一个test方法来给Label.text赋值

OC:

#import "Viewcontrller"

@interface ViewController ()

@prperty (nonatomic, weak) IBoutlet UILabel *label;

@end

@implementation ViewController

- (void)ViewDidLoad {
    [super ViewDidLoad];

    [self test];
}

- (void)test {
    self.label.text = @"测试";
}

@end

Swift:


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.test()
    }

    dynamic func test() {
        label.text = "测试";
    }
}


2:打开JSPatch网站下载SDK

JSPatchSDK

iOS 快速集成热修复(JSPatch)_第1张图片
JSPatch.png

3:项目配置

将解压的SDK直接拖入工程中, 然后在TARGETS -> Build Phases -> Link Binary With Libraries -> + 添加libz.dulib(或libz.tad)和JavaScriptCore.framework.

iOS 快速集成热修复(JSPatch)_第2张图片
导入.png
iOS 快速集成热修复(JSPatch)_第3张图片
项目配置.png

在AppDelegate里写以下代码:

  • OC
#import 
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [JSPatch startWithAppKey:@"你的AppKey"];
    [JSPatch setupDevelopment];
    [JSPatch sync];
    ...
    return YES;
}
@end
  • Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        JSPatch.startWithAppKey("你的AppKey")
        JSPatch.setupDevelopment();
        JSPatch.sync()
        ...
        return true
    }

至此 JSPatch 接入完毕,下一步可以开始在后台为这个 App 添加 JS 补丁文件了


4:打开JSPatch官网注册

注册

iOS 快速集成热修复(JSPatch)_第4张图片
注册.png

5:创建你的app, 名字可以随便写, AppID可填可不填

iOS 快速集成热修复(JSPatch)_第5张图片
![添加App.png](http://upload-images.jianshu.io/upload_images/2012472-a07eb158b0366576.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

6创建之后的APP

iOS 快速集成热修复(JSPatch)_第6张图片
![添加版本.png](http://upload-images.jianshu.io/upload_images/2012472-8a493f963ce3f9f9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  • appKey是项目中用到的(一般第三方都有个appkey)
  • App版本必须与你App的版本保持一致, 否则会无效
iOS 快速集成热修复(JSPatch)_第7张图片
Version.png

7:创建一个main.js文件并在里面写上以下代码

  • OC:
//  ViewController 需要修改的控制器
denfineClass('ViewController', {
    test: function() {
        self.label().setText("通过JSPatch修改");
    }
})
  • Swift
denfineClass('JSPatchDemo.ViewController', {
    test: function() {
        self.label().setText("通过JSPatch修改");
    }
})

Swift覆盖方法和类的时候要加上项目名, 所以规范应该是项目名.类名(方法名)注册类的时候也要加上项目名


8:点击刚刚创建的1.0, 将保存好的js上传到JSPatch服务器上.

![发布.png](http://upload-images.jianshu.io/upload_images/2012472-f8ab9fead19ae873.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

选择文件->选择写好的main.js文件->勾选开发预览(测试ok全量下发)->提交

出现以下内容说明项目已经更新补丁

iOS 快速集成热修复(JSPatch)_第8张图片
更新补丁.png

因为补丁是先下载保存, 下次运行才会生效

效果

iOS 快速集成热修复(JSPatch)_第9张图片
效果.png

你可能感兴趣的:(iOS 快速集成热修复(JSPatch))