ios 动态注入代码
1.mac安装xcode 插件 下载地址
2.工程引入.a文件 (可在下载地址的文件夹中找到工程,请自行编译,或则pod 搜索 dyci)
3.配置工程环境:
a.buildsettings: Enable Code Coverage Support 改为NO
b.buildsettings:Other Link Flags : -force_load 加 .a所在路径
4.需要动态注入代码的类中添加方法1
2
3
4
5
6
7- (void)updateOnClassInjection {
//在这里注入代码
}
- (void)updateOnResourceInjection:(NSString *)resourcePath {
//在这里注入代码
}
5.control+x 实施注入。
注意事项:
安装的时候,请关闭xcode,如果安装成功,打开xcode,会提示是否加载未经apple认证的plugin,点是 就ok了。
代码中不需要import 任何文件
目前仅适用于模拟器,不支持真机。
该插件主要方便快速开发,调整UI等。
补充:
实施注入的时候偶尔会失败,出现如下提示:
Cannot inject this file right now. If you think that this file is injectable, try again bit later
如果等几秒钟之后依然如此,请确认设置里面的配置是否都正确
如果加载成功,app启动会打印如下信息:1
2
3
4
5
6
7
8DYCI directory path is :
DYCI : Dynamic Code Injection was started...
To disable it, paste next line in your application:didFinishLaunching: method :
[NSClassFromString(@"SFDynamicCodeInjection") performSelector:@selector(disable)];
or
imply remove dyci from dependencies