iOS相关技术iOS进阶指南移动开发

iOS 快速集成热修复(JSPatch)

2016-08-29  本文已影响193人  H了个L

前言

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

正题

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

JSPatch.png

3:项目配置

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

导入.png 项目配置.png

在AppDelegate里写以下代码:

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

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


4:打开JSPatch官网注册

注册

注册.png

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

![添加App.png](http:https://img.haomeiwen.com/i2012472/a07eb158b0366576.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

6创建之后的APP

![添加版本.png](http:https://img.haomeiwen.com/i2012472/8a493f963ce3f9f9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) Version.png

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

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

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


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

![发布.png](http:https://img.haomeiwen.com/i2012472/f8ab9fead19ae873.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

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

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

更新补丁.png

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

效果

效果.png
上一篇 下一篇

猜你喜欢

热点阅读