3. 选择的项目会在Open Recent中出现, 保持File Watcher的选项勾选.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
#ifdef DEBUG
//InjectionIII 注入
[[NSBundle bundleWithPath:@"/Applications/InjectionIII.app/Contents/Resources/iOSInjection.bundle"] load];
#else
#endif
return YES;
}
5. 在需要动态调试的页面控制器中写上injected
方法, 把需要操作的UI方法添加到injected
中执行, 如果想让全部的控制器都能使用, 直接添加到BaseViewController.
// Objective-C:
- (void)injected {
#ifdef DEBUG
NSLog(@"I've been injected: %@", self);
[self viewDidLoad];
#endif
}
// Swift
@objc func injected() {
#if DEBUG
print("I've been injected: \(self)")
self.viewDidLoad()
#endif
}
6. 重新编译项目, 控制台可以看到
** InjectionIII connected /Users/***/Desktop/***/**/***.xcworkspace**
** Watching files under /Users/***/Desktop/****
// 下面的只是警告, 作者在Issue中已经解释, 不耽误正常使用.
** ⚠️ Your project file seems to be in the Desktop or Documents folder and may prevent InjectionIII working as it has special permissions.**
7. 修改完UI, 直接cmd + S
就能看到效果, 部分页面可能耗时比较久或无法使用, 正常页面均能使用.
Injection for XCode
GitHub地址:
https://github.com/johnno1962/InjectionIII
Injection工具可以动态地将iOS代码在已运行的程序中执行, 不用重启.
Injection会监听源代码文件的变化, 如果文件被改动了,
Injection Server就会执行rebuildClass重新进行编译、打包成动态库.dylib文件,
编译、打包成动态库后, 使用writeString方法通过Socket通知运行的App.
- (BOOL)writeString:(NSString *)string { const char *utf8 = string.UTF8String; uint32_t length = (uint32_t)strlen(utf8); if (write(clientSocket, &length, sizeof length) != sizeof length || write(clientSocket, utf8, length) != length) return FALSE; return TRUE; }
Server会在后台发送和监听Socket消息, Client也会开启一个后台去发送和监听Socket消息.
Client接收到消息后会调用inject(tmpfile: String)
方法, 运行时进行类的动态替换(新类动态替换旧类).
dlopen
会把tmpfile动态库文件载入运行的App里, 返回指针dl.
接下来, dlsym会得到tmpfile动态库的符号地址, 然后就可以处理类的替换工作了.
当类的方法都被替换后, 我们就可以开始重新绘制界面了.
使用动态库方式极速调试, 整个过程无需重新编译和重启App.