美文网首页
iOS动态调试代码利器InjectionIII使用

iOS动态调试代码利器InjectionIII使用

作者: jayhe | 来源:发表于2019-04-30 16:29 被阅读0次

1. InjectionIII是什么?

InjectionIII是一个能在模拟器上,监听代码变化,不需要rebuild整个项目就能更新代码变化效果的工具;当代码修改之后,我们只需要保存就能触发修改类的injected方法或者触发通知INJECTION_BUNDLE_NOTIFICATION,前提是你在对应的类中实现injected函数或者监听INJECTION_BUNDLE_NOTIFICATION通知

2.InjectionIII如何使用

1. 安装InjectionIII.app
  1. 在AppStore下载APP
    Mac app store
  2. clone代码手动build APP
  • clone代码

git clone https://github.com/johnno1962/InjectionIII --recurse-submodules

  • 打包app
    打开项目,修改签名、修改脚本中关于Xcode的位置,注意这里修改成你的Xcode的名称,源码是Xcode101.app


    1556610439783.png
  • 将打包的app拷贝到应用程序中,也可以设置开机运行这个app

2. 集成到项目中

在AppDelegate中加上如下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
#if DEBUG
    __unused BOOL isLoaded = [[NSBundle bundleWithPath:@"/Applications/InjectionIII.app/Contents/Resources/iOSInjection10.bundle"] load];
#endif
    return YES;
}
3. 使用
  1. 在InjectionIII app的操作菜单中指定需要监听变化的项目目录


    1556611117233.png
    1556611155038.png
  1. 在需要Inject的类中添加injected函数,或者观察通知
    通知的方式
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(injectedAction) name:@"INJECTION_BUNDLE_NOTIFICATION" object:nil];
    }
    
    return self;
}

- (void)injectedAction {
    // 这里调用需要执行的代码
}

函数的方式

- (void)injected {
    [self setupUI];
}

- (void)setupUI {
    // 这里调用需要执行的代码:比如这里修改控制器的背景色
    self.view.backgroundColor = [UIColor purpleColor];
    NSLog(@"Injected %s", __FUNCTION__);
}
  1. 修改代码之后,cmd+s就可以看到injected的代码执行了,这样不用rebuild就能看到变化效果

3.QA

  1. 日志输出Loaded .dylib - Ignore any duplicate class warning ^
💉 *** Compiling /Users/hechao/Documents/Demos/RuntimeLearning/RuntimeLearning/ViewController.m ***
💉 Loading .dylib ...
objc[28267]: Class ViewController is implemented in both /Users/hechao/Library/Developer/CoreSimulator/Devices/EEBF7CD0-134A-44D7-8B7E-70C1EEC94BAC/data/Containers/Bundle/Application/649CD362-7823-4397-A649-34128DBA4CC7/RuntimeLearning.app/RuntimeLearning (0x10a3defa8) and /Users/hechao/Library/Containers/com.johnholdsworth.InjectionIII/Data/eval102.dylib (0x12cfb2568). One of the two will be used. Which one is undefined.
💉 Loaded .dylib - Ignore any duplicate class warning ^

这个问题不用处理,不影响使用

  1. 在用到RAC库的项目中,inject直接crash


    1556611927298.png

去github上查看crash issue找到关键信息如下;有RAC的项目使用通知观察INJECTION_BUNDLE_NOTIFICATION通知


Thanks for the information on your crash. Last time I checked it’s not possible to use InjectionIII with ReactiveObjC as it uses __unsafe_unretained pointers which can be deallocated when injection does it’s “sweep” which implements the - (void)injected functionality. The alternative is to subscribe to the “INJECTION_BUNDLE_NOTIFICATION” notification.
  1. pod中的变动,保存报错
    这个没有去验证

把 File -> Workspace Setting -> Build System, 改为Legacy Build System模式,默认的New Build System(Default)模式,是不会编译pod 里面的改动的

总之有问题直接去github上查issue中有没有类似的问题,看是否已经有解决方案;没有的话就提issue了

相关文章

网友评论

      本文标题:iOS动态调试代码利器InjectionIII使用

      本文链接:https://www.haomeiwen.com/subject/rdmvnqtx.html