Use method swizzling to extract duplicate operations
by Wusheying
Repeat operations such as data escalation, public features, and so on.
1. Establishing the category of Uiviewcontroller
2. Code:
#import "Uiviewcontroller+analytics.h" #import <objc/runtime.h> @implementation uiviewcontroller (Analytics)+ (void) Load {Static dispatch_once_tOncetoken;dispatch_once(&oncetoken, ^{class class = [ SelfClass]; SEL Originalselector =@selector(viewwillappear:); SEL Swizzledselector =@selector(ana_viewwillappear:); Method Originalmethod = Class_getinstancemethod (class, Originalselector); Method Swizzledmethod = Class_getinstancemethod (class, Swizzledselector);BOOLSuccess = Class_addmethod (class, Originalselector, Method_getimplementation (Swizzledmethod), method_gettypeencoding (Swizzledmethod));if(success) {Class_replacemethod (class, Swizzledselector, Method_getimplementation (Originalmethod), method_gettypeencoding (Originalmethod)); }Else{method_exchangeimplementations (Originalmethod, Swizzledmethod); } });} - (void) Ana_viewwillappear: (BOOL) Animated {[ SelfAna_viewwillappear:animated];NSLog(@"Ana_viewwillappear");NSLog(@"%@", Self); Mta_report (); [ SelfHidenavigationshadowline];}@end
3. Output
2015-06-16 09:17:59.838 test[37292:2748514] Ana_viewwillappear
2015-06-16 09:17:59.838 test[37292:2748514] viewcontroller:0x7fb7515291c0>
Use method swizzling to extract duplicate operations