1. InjectionIII是什么誊爹?
InjectionIII是一個(gè)能在模擬器上,監(jiān)聽(tīng)代碼變化瓢捉,不需要rebuild整個(gè)項(xiàng)目就能更新代碼變化效果的工具频丘;當(dāng)代碼修改之后,我們只需要保存就能觸發(fā)修改類的injected方法或者觸發(fā)通知
INJECTION_BUNDLE_NOTIFICATION
泡态,前提是你在對(duì)應(yīng)的類中實(shí)現(xiàn)injected函數(shù)或者監(jiān)聽(tīng)INJECTION_BUNDLE_NOTIFICATION
通知
2.InjectionIII如何使用
1. 安裝InjectionIII.app
- 在AppStore下載APP
Mac app store - clone代碼手動(dòng)build APP
- clone代碼
git clone https://github.com/johnno1962/InjectionIII --recurse-submodules
-
打包app
打開(kāi)項(xiàng)目搂漠,修改簽名、修改腳本中關(guān)于Xcode的位置兽赁,注意這里修改成你的Xcode的名稱状答,源碼是Xcode101.app
1556610439783.png 將打包的app拷貝到應(yīng)用程序中冷守,也可以設(shè)置開(kāi)機(jī)運(yùn)行這個(gè)app
2. 集成到項(xiàng)目中
在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. 使用
-
在InjectionIII app的操作菜單中指定需要監(jiān)聽(tīng)變化的項(xiàng)目目錄
1556611117233.png
1556611155038.png
- 在需要Inject的類中添加injected函數(shù)刀崖,或者觀察通知
通知的方式
- (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 {
// 這里調(diào)用需要執(zhí)行的代碼
}
函數(shù)的方式
- (void)injected {
[self setupUI];
}
- (void)setupUI {
// 這里調(diào)用需要執(zhí)行的代碼:比如這里修改控制器的背景色
self.view.backgroundColor = [UIColor purpleColor];
NSLog(@"Injected %s", __FUNCTION__);
}
- 修改代碼之后,cmd+s就可以看到injected的代碼執(zhí)行了拍摇,這樣不用rebuild就能看到變化效果
3.QA
- 日志輸出
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 ^
這個(gè)問(wèn)題不用處理亮钦,不影響使用
-
在用到RAC庫(kù)的項(xiàng)目中,inject直接crash
1556611927298.png
去github上查看crash issue找到關(guān)鍵信息如下充活;有RAC的項(xiàng)目使用通知觀察
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.
- pod中的變動(dòng)蜂莉,保存報(bào)錯(cuò)
這個(gè)沒(méi)有去驗(yàn)證
把 File -> Workspace Setting -> Build System, 改為L(zhǎng)egacy Build System模式混卵,默認(rèn)的New Build System(Default)模式映穗,是不會(huì)編譯pod 里面的改動(dòng)的
總之有問(wèn)題直接去github上查issue中有沒(méi)有類似的問(wèn)題,看是否已經(jīng)有解決方案幕随;沒(méi)有的話就提issue了