簡介
一個簡潔高效的用于使iOS支持AOP面向切面編程的庫.它可以幫助你在不改變一個類或類實例的代碼的前提下,有效更改類的行為.比iOS傳統的 AOP方法,更加簡單高效.支持在方法執行的前/后或替代原方法執行.曾經是 PSPDFKit 的一部分,PSPDFKit,在Dropbox和Evernote中都有應用,現在單獨單獨開源出來給大家使用.
安裝
使用 CocoaPods 安裝
pod "Aspects"
手動安裝
把文件Aspects.h/m
拖到工程中即可.
用法
應用場景
Aspects 用于支持AOP(面向切面編程)模式,用于部分解決OOP(面向對象)模式無法解決的特定問題.具體指的是那些在多個方法有交叉,無法或很難被有效歸類的操作,比如:
不論何時用戶通過客戶端獲取服務器端數據,權限檢查總是必須的.不論何時用戶和市場交互,總應該更具用戶的操作提供相應地購買參考或相關商品.所有需要日志記錄的操作.
接口概述
Aspects 給 NSObject 擴展了下面的方法:
/// 為一個指定的類的某個方法執行前/替換/后,添加一段代碼塊.對這個類的所有對象都會起作用.////// @param block 方法被添加鉤子時,Aspectes會拷貝方法的簽名信息./// 第一個參數將會是 id<AspectInfo>
,余下的參數是此被調用的方法的參數./// 這些參數是可選的,并將被用于傳遞給block代碼塊對應位置的參數./// 你甚至使用一個沒有任何參數或只有一個id<AspectInfo>
參數的block代碼塊.////// @注意 不支持給靜態方法添加鉤子./// @return 返回一個唯一值,用于取消此鉤子.+ (id<AspectToken>)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error;/// 為一個指定的對象的某個方法執行前/替換/后,添加一段代碼塊.只作用于當前對象. - (id<AspectToken>)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error; - (id<AspectToken>)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error; /// 撤銷一個Aspect 鉤子./// @return YES 撤銷成功, 否則返回 NO. id<AspectToken> aspect = ...; [aspect remove];
所有的調用,都會是線程安全的.Aspects 使用了Objective-C 的消息轉發機會,會有一定的性能消耗.所有對于過于頻繁的調用,不建議使用 Aspects.Aspects更適用于視圖/控制器相關的等每秒調用不超過1000次的代碼.
代碼示例
可以在調試應用時,使用Aspects動態添加日志記錄功能.
[UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, BOOL animated) { NSLog(@"控制器 %@ 將要顯示: %tu", aspectInfo.instance, animated);} error:NULL];
使用它,分析功能的設置會很簡單:https://github.com/orta/ARAnalytics
你可以在你的測試用例中用它來檢查某個方法是否被真正調用(當涉及到繼承或類目擴展時,很容易發生某個父類/子類方法未按預期調用的情況):
- (void)testExample { TestClass *testClass = [TestClass new]; TestClass *testClass2 = [TestClass new]; __block BOOL testCallCalled = NO; [testClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionAfter usingBlock:^{ testCallCalled = YES; } error:NULL]; [testClass2 testCallAndExecuteBlock:^{ [testClass testCall]; } error:NULL]; XCTAssertTrue(testCallCalled, @"調用testCallAndExecuteBlock 必須調用 testCall");}
它對調試應用真的會提供很大的作用.這里我想要知道究竟何時輕擊手勢的狀態發生變化(如果是某個你自定義的手勢的子類,你可以重寫setState:方法來達到類似的效果;但這里的真正目的是,捕捉所有的各類控件的輕擊手勢,以準確分析原因):
[_singleTapGesture aspect_hookSelector:@selector(setState:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) { NSLog(@"%@: %@", aspectInfo.instance, aspectInfo.arguments);} error:NULL];
下面是一個你監測一個模態顯示的控制器何時消失的示例.通常,你也可以寫一個子類,來實現相似的效果,但使用 Aspects 可以有效減小你的代碼量:
@implementation UIViewController (DismissActionHook)// Will add a dismiss action once the controller gets dismissed.- (void)pspdf_addWillDismissAction:(void (^)(void))action { PSPDFAssert(action != NULL); [self aspect_hookSelector:@selector(viewWillDisappear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) { if ([aspectInfo.instance isBeingDismissed]) { action(); } } error:NULL];}@end
對調試的好處
Aspectes 會自動標記自己,所有很容易在調用棧中查看某個方法是否已經調用:
在返回值不為void的方法上使用 Aspects
你可以使用 NSInvocation 對象類自定義返回值:
[PSPDFDrawView aspect_hookSelector:@selector(shouldProcessTouches:withEvent:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info, NSSet *touches, UIEvent *event) { // 調用方法原來的實現. BOOL processTouches; NSInvocation *invocation = info.originalInvocation; [invocation invoke]; [invocation getReturnValue:&processTouches]; if (processTouches) { processTouches = pspdf_stylusShouldProcessTouches(touches, event); [invocation setReturnValue:&processTouches]; } } error:NULL];
兼容性與限制
當應用于某個類時(使用類方法添加鉤子),不能同時hook父類和子類的同一個方法;否則會引起循環調用問題.但是,當應用于某個類的示例時(使用實例方法添加鉤子),不受此限制.使用KVO時,最好在aspect_hookSelector:
調用之后添加觀察者;否則可能會引起崩潰.