直接看代码
咱们村的人都知道NSObject是爹,那就让给爹加俩方法,这样所有的儿子都能进行swizzle
#import <Foundation/Foundation.h>
@interface NSObject (WPSwizzle)
//替换对象方法
+ (void)swizzleInstanceSelector:(SEL)origSelector withNewSelector:(SEL)newSelector;
//替换类方法
+ (void)swizzleClassSelector:(SEL)origSelector withNewSelector:(SEL)newSelector;
@end
#import "NSObject+WPSwizzle.h"
#import <objc/runtime.h>
@implementation NSObject (WPSwizzle)
//替换对象方法
+ (void)swizzleInstanceSelector:(SEL)origSelector withNewSelector:(SEL)newSelector{
Method method1 = class_getInstanceMethod([self class], origSelector);
Method method2 = class_getInstanceMethod([self class], newSelector);
method_exchangeImplementations(method1, method2);
}
//替换类方法
+ (void)swizzleClassSelector:(SEL)origSelector withNewSelector:(SEL)newSelector{
Method method1 = class_getClassMethod([self class], origSelector);
Method method2 = class_getClassMethod([self class], newSelector);
method_exchangeImplementations(method1, method2);
}
@end
接下来儿子们干活,添加页面统计路径,已类名为页面路径名
//
// UIViewController+UMengViewPage.h
// Identify
//
// Created by wupeng on 16/3/7.
// Copyright © 2016年 StarShine. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIViewController (UMengViewPage)
@end
#import "UIViewController+UMengViewPage.h"
#import <UMengAnalytics-NO-IDFA/MobClick.h>
#import "NSObject+WPSwizzle.h"
@implementation UIViewController (UMengViewPage)
// 分类并不推荐
//-(void)viewWillAppear:(BOOL)animated {
// if ([self isKindOfClass:[UINavigationController class]] || [self isKindOfClass:[UITabBarController class]]) {
//
// } else {
// [MobClick beginLogPageView:NSStringFromClass([self class])];
// }
//}
//-(void)viewDidDisappear:(BOOL)animated {
// if ([self isKindOfClass:[UINavigationController class]] || [self isKindOfClass:[UITabBarController class]]) {
//
// } else {
// [MobClick endLogPageView:NSStringFromClass([self class])];
// }
//}
+(void)load {
[self swizzleInstanceSelector:@selector(viewWillAppear:) withNewSelector:@selector(wp_viewWillAppear:)];
[self swizzleInstanceSelector:@selector(viewDidDisappear:) withNewSelector:@selector(wp_viewDidDisappear:)];
}
-(void)wp_viewWillAppear:(BOOL)animated {
[self wp_viewWillAppear:true];
if ([self isKindOfClass:[UINavigationController class]] || [self isKindOfClass:[UITabBarController class]] || [self isKindOfClass:[UIInputViewController class]] ||
[self isKindOfClass:[UIAlertController class]]) {
} else {
NSString *vcName = NSStringFromClass([self class]);
if (![vcName isEqualToString:@"UIInputWindowController"]) {
[MobClick beginLogPageView:vcName];
}
}
}
-(void)wp_viewDidDisappear:(BOOL)animated {
[self wp_viewDidDisappear:true];
if ([self isKindOfClass:[UINavigationController class]] || [self isKindOfClass:[UITabBarController class]] || [self isKindOfClass:[UIInputViewController class]] ||
[self isKindOfClass:[UIAlertController class]]) {
} else {
NSString *vcName = NSStringFromClass([self class]);
if (![vcName isEqualToString:@"UIInputWindowController"]) {
[MobClick endLogPageView:vcName];
}
}
}
@end