最近一段时间苦恼于UI调试,基本上就是麻木的修改运行Xcode,修改再运行Xcode,苦恼的地方在于项目文件逐渐变大、电脑性能变差造成App编译时间变长,还有就是如果调试的页面层级较深,再跳转到指定页面又是一波操作,很费时间。
今天看到一个介绍 热重载
的帖子,可以直接看到修改效果,无需重新编译,顿时有种天晴了的感觉,决定尝试并记录使用方法。
首先是原理介绍文章:
Injection:iOS热重载背后的黑魔法
Injection
也在Github上 https://github.com/johnno1962/injectionforxcode。
使用方法也简单:
先去商店下载并启动App
Injection.png
然后在 application:DidFinishLaunching:
方法中加入如下代码:
#if DEBUG
// iOS
[[NSBundle bundleWithPath:@"/Applications/InjectionIII.app/Contents/Resources/iOSInjection.bundle"] load];
// tvOS
// [[NSBundle bundleWithPath:@"/Applications/InjectionIII.app/Contents/Resources/tvOSInjection.bundle"] load];
// macOS
// [[NSBundle bundleWithPath:@"/Applications/InjectionIII.app/Contents/Resources/macOSInjection.bundle"] load];
#endif
最后,在自定义文件中添加方法 injected
:
- (void)injected
{
NSLog(@"I've been injected: %@", self);
// 自定义修改
// .....
}
使用快捷键 command + s
快捷键,之后App界面就会做出相应的修改。
当前 Injection
只能在模拟器上使用。