寫tweak的時(shí)候歇拆,經(jīng)常需要tweak進(jìn)程間通訊鞋屈。因?yàn)閔ook的應(yīng)用經(jīng)常都是在沙盒里的,如果需要操作沙盒外的文件故觅,比如記錄日志什么的厂庇,就不行。
怎么處理呢输吏?
就是在有權(quán)限的地方权旷,比如SpringBoard,或者守護(hù)進(jìn)程里贯溅,起一個(gè)服務(wù)拄氯。在沙盒里,發(fā)送一個(gè)通知它浅,然后被服務(wù)捕獲到坤邪。在服務(wù)里處理邏輯。
這里我們講一下基于rocketbootstrap的進(jìn)程間通訊罚缕。
rocketbootstrap是別人已經(jīng)封裝好的工具,我們可以直接拿來用怎静。
參照文檔:
http://iphonedevwiki.net/index.php/Updating_extensions_for_iOS_7
廢話不多說邮弹,直接寫demo。
首先蚓聘,建立一個(gè)iOS OpenDev的tweak項(xiàng)目腌乡,導(dǎo)入。
這些文件可以直接在github上獲取夜牡。
頭文件:https://github.com/rpetrich/RocketBootstrap/tree/master
dylib我是直接在手機(jī)里提取的, 在cydia中安裝RocketBootstrap之后与纽,直接可以找到
文件路徑:/usr/lib/librocketbootstrap.dylib
接下來寫xm文件。
首選在SpringBoard中塘装,將RocketBootstrap的服務(wù)起了急迂。
具體代碼:
#TweakDemo.xm
#import "rocketbootstrap.h"
#define kXPCCenterNameKey @"kXPCCenterNameKey_83641"
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application {
%orig;
CPDistributedMessagingCenter *c = [%c(CPDistributedMessagingCenter) centerNamed:kXPCCenterNameKey];
rocketbootstrap_distributedmessagingcenter_apply(c);
[c runServerOnCurrentThread];
[c registerForMessageName:@"myMessageName" target:self selector:@selector(handleMessage:withUserInfo:)];
NSLog(@"注冊(cè)監(jiān)聽 start");
}
%new
- (void)handleMessage:(NSString *)name withUserInfo:(NSDictionary *)userInfo {
NSLog(@"handleMessage withUserInfo:%@",userInfo);
//TODO:something
}
%end
在需要發(fā)送的地方,如此這般的寫:
%hook SomeClass
-(void)someMethod{
%orig;
NSMutableDictionary *userInfo = [@{} mutableCopy];
[userInfo setObject:@"123" forKey:@"arg001"];
[userInfo setObject:@"456" forKey:@"arg002"];
NSLog(@"發(fā)送: %@=%@",kXPCCenterNameKey,userInfo);
CPDistributedMessagingCenter *c = [%c(CPDistributedMessagingCenter) centerNamed:kXPCCenterNameKey];
rocketbootstrap_distributedmessagingcenter_apply(c);
[c sendMessageName:@"myMessageName" userInfo:userInfo];
}
%end
因?yàn)槲覀兊膖weak是依賴RocketBootstrap的蹦肴,所以需要在control文件里面配置一下Depends:
Depends: firmware (>= 5.0), mobilesubstrate,com.rpetrich.rocketbootstrap (>= 1.0.2) | firmware (<< 7.0)
跑一下試試看僚碎,祝你成功。