ARTICLE · 1060359
插件使用Swift Orion来开发的见解与讲解
插件使用Swift Orion来开发的见解与讲解swift自从到了swift5版本之后,就很稳定了。 而且swift代码相较于oc是比较清爽的,项目模块中的方法与属性是全局性的,不需要像oc那样需要引入才能使用。也不需要像oc那样需要声明“**.h”文件和“**.m”文件搭配使用。调用方法也是更为清爽。 所以我还是呼吁大家去拥抱现代高阶语言,学习起来也是更为简单,用起来也是更容易上手。 现在给一些theos中的代码对比吧,帮助大家能在orion中少走点弯路: 一、普通方法 hook logos: 在swift orion中等同于: 二、类方法 hook logos: 在swift orion中等同于: 三、新增方法 logos: 在swift orion中等同于: 四、hook swift 模块所暴露的方法 logos: 在swift orion中等同于: 当然,可以可以用group更安全(此处演示Orion 的group方式, 后续就不演示它与logos的group区别了): 五、动态调用未声明的方法(普通方法) logos: 在swift orion中等同于: 六、动态调用未声明的方法(类方法) logos: 在swift orion中等同于: 七、funcation hook logos: 在swift orion中等同于: 二者在hook方式上有些区别,有时候logos繁琐,有时候orion繁琐,这个得适应。但是在业务逻辑上的实现,swift显然更舒服一些。 其他的我就不一一举例了,具体的可以看theos 官方文档:
%hook SpringBoard-(void)applicationDidFinishLaunching: (id)arg {%orig;}%end
class SpringBoardHook: ClassHook<SpringBoard> {func applicationDidFinishLaunching(_ arg: AnyObject?) {orig.applicationDidFinishLaunching(arg);}}
%hook UIApplication+ (id)sharedApplication {return %orig;}%end
classUIApplicationHook: ClassHook<UIApplication> {classfuncsharedApplication() -> AnyObject? {return orig.sharedApplication();}}
%hook SpringBoard%new-(void)someFunc {}%end
classSpringBoardHook: ClassHook<SpringBoard> {// orion:newfunc someFunc() {}}
@interface ViewController: UIViewController-(void) sendMsg;@end%hook ViewController-(void) sendMsg {// todo}%end%ctor {// 当然此处也可以用%c代替NSClassFromStringClass vcClass = NSClassFromString(@"TargetXxxxxxx.ViewController");if (vcClass) {%init(ViewController = vcClass);}}
class ViewControllerHook: ClassHook<NSObject> {static let targetName = "TargetXxxxxxx.ViewController";func sendMsg() {// todo}}
struct vcGroup: HookGroup {let classString: String;}// 当然此处Hook对象并不一定都是指定NSObject,如果目标类最终是继承自UIView,那么就写UIViewclass ViewControllerHook: ClassHook<NSObject> {typealias Group = vcGroup;static let targetName = group.classString;func sendMsg() {// todo}}class TweakInit: Tweak {required init() {if let _: AnyClass = NSClassFromString("TargetXxxxxxx.ViewController") {vcGroup(classString: "TargetXxxxxxx.ViewController").activate();}}}
@interface ViewControllerManager-(void) sendMsg;@endid vc = [NSClassFromString(@"ViewControllerManager") new];[vc sendMsg];
// 因为swift与oc的差异性,所以得需要一个协议来桥接@objc protocol MyViewControllerManager {func sendMsg;}// 调用 ViewControllerManager sendMsg 方法let vcManager = Dynamic.ViewControllerManager.alloc(interface: MyViewControllerManager.self).init();vcManager.sendMsg();
@interfaceViewControllerMgr+(void) sendMsg;@end// 应该是这样写,好久没用oc了,有点忘记了。[NSClassFromString(@"ViewControllerMgr") sendMsg];
// 因为swift与oc的差异性,所以得需要一个协议来桥接@objc protocol MyViewControllerManager {func sendMsg;}// 调用 ViewControllerManager sendMsg 方法// 和普通方法有点区别,请仔细对比let vcManager = Dynamic.ViewControllerManager.as(interface: MyViewControllerManager.self);vcManager.sendMsg();
%hookf(size_t, strlen, const char* val) {return %orig;}
class StrlenHook: FunctionHook {static let target = Function.symbol("strlen", image: nil);// swift中的数据类型,并不完全等同于c中的指针类型,具体转换成什么类型需要自己去科普func function(_ val: UnsafePointer<Int8>) -> UInt {return orig.function(val);}}
https://orion.theos.dev/
动态语言一时爽,代码重构火葬场。