iOS热修复(阿里EMAS)

2022-04-27  本文已影响0人  心猿意码_

一、目标

二、操作步骤

准备工作
项目接入及使用
# 热修复
 pod 'AlicloudLua'
 pod 'AlicloudBeacon'
 pod 'AlicloudUtils'
 pod 'ZipArchive', '~> 1.4.0'
 pod 'AlicloudHotFixDebug', '~> 1.0.1'  # 本地调试
#import "AppDelegate.h"
// 热更新
#import <AlicloudHotFix/AlicloudHotFix.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ViewController *vc = [[ViewController alloc] init];
    _navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window setRootViewController:_navigationController];
    [_window setBackgroundColor:[UIColor whiteColor]];
    [_window makeKeyAndVisible];
    
    // 热修复
    [self hotfixSdkInit];
    return YES;
}



// 热修复
- (void)hotfixSdkInit {
    AlicloudHotFixService *hotfixService = [AlicloudHotFixService sharedInstance];
    // 打开Log
    [hotfixService setLogEnabled:YES];
    // 手动设置App版本号
    [hotfixService setAppVersion:@"1.0"];
    [hotfixService initWithAppId:appId appSecret:appSecret rsaPrivateKey:appRsaPrivateKey callback:^(BOOL res, id data, NSError *error) {
        if (res) {
            NSLog(@"HotFix SDK init success.");
        } else {
            NSLog(@"HotFix SDK init failed, error: %@", error);
        }
    }];
}

示例:本地校验测试

interface{"EMASCrashVC"}

function aClick(self)
local arrary = {"1","2"}
local str = arrary[1]
print(str)
self:view():setBackgroundColor(UIColor:redColorr())
end

对应项目中崩溃的方法:

//
// EMASCrashVC.m
// crash
- (void)aClick{
  NSArray *array = @[];
  NSString *str = array[1];
  NSLog(@"%@",str);
}
NSString *patch = [[NSBundle mainBundle] pathForResource:@"EMASCrash" ofType:@"lua"];
[AlicloudHotFixDebugService loadLocalPachFile:patch];

结果:项目中崩溃的方法(- (void)aClick)不再崩溃,打印值:1,背景变成红色。

示例:线上使用


[[AlicloudHotFixService sharedInstance] loadPatch:^(BOOL res, id data, NSError *error) {
    if (res) {
      NSLog(@"Load patch success.");
    } else {
      NSLog(@"Load patch failed, error: %@", error);
    }
  }];

结果返回:Load patch success. 即完成修复
注:使用过程中出现个问题,“线上场景”使用模拟器运行无法热修复成功,须使用真机运行;本地热修复使用真机/模拟器均可。

以下是效果演示


热修复.gif

三、遇坑总结

上一篇 下一篇

猜你喜欢

热点阅读