UIViewController+Swizzling.m 4.8 KB
//
//  UIViewController+Swizzling.m
//  PDA
//
//  Created by KiTee on 2018/9/7.
//  Copyright © 2018年 Linno. All rights reserved.
//

#import "UIViewController+Swizzling.h"
#import "BaseNaviController.h"
#import <objc/runtime.h>

@implementation UIViewController (Swizzling)

+ (void)load
{
    // 拦截viewWillAppear:事件
    SEL origSel0 = @selector(viewWillAppear:);
    SEL swizSel0 = @selector(swiz_viewWillAppear:);
    [UIViewController swizzleMethods:[self class] originalSelector:origSel0 swizzledSelector:swizSel0];
    
    // 拦截viewDidLoad事件
    SEL origSel = @selector(viewDidLoad);
    SEL swizSel = @selector(swiz_viewDidLoad:);
    [UIViewController swizzleMethods:[self class] originalSelector:origSel swizzledSelector:swizSel];
    
    // 拦截viewDidAppear:事件
    SEL origSel2 = @selector(viewDidAppear:);
    SEL swizSel2 = @selector(swiz_viewDidAppear:);
    [UIViewController swizzleMethods:[self class] originalSelector:origSel2 swizzledSelector:swizSel2];
}

// exchange implementation of two methods
+ (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel
{
    Method origMethod = class_getInstanceMethod(class, origSel);
    Method swizMethod = class_getInstanceMethod(class, swizSel);
    
    // class_addMethod will fail if original method already exists
    BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
    if (didAddMethod) {
        class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        // origMethod and swizMethod already exist
        method_exchangeImplementations(origMethod, swizMethod);
    }
}

#pragma mark - 响应自定义viewWillAppear:事件
- (void)swiz_viewWillAppear:(BOOL)animated {
#pragma mark - 处理导航栏显示/隐藏
    self.navigationController.navigationBarHidden = ([self respondsToSelector:@selector(isShowNavigationBar)] && [self isShowNavigationBar] == YES) ? YES : NO;
    
#pragma mark - 根据不同页面设置不同的导航栏风格
    if ([self respondsToSelector:@selector(navigationBarStyle)]) {
        [BaseNaviController setNavigationBarStyle:[self navigationBarStyle] vc:self];
    }
    // 系统拍照和相册页面调试
    // NSString *className = NSStringFromClass([self class]);
    // NSArray *classArrary = @[@"JWMImagePickerController",
    //                             @"CAMImagePickerCameraViewController", @"CAMViewfinderViewController", @"CAMPreviewViewController",
    //                             @"UIInputWindowController", @"UIEditingOverlayViewController", @"PUPhotoPickerHostViewController",
    //                             @"PLPhotoTileViewController"];
    
#pragma mark - 处理用户打开自动切换主题按钮,根据设定好的时间段进行主题切换
    if (kGetUserDefaultsBOOL(ThemeAutoSwitch)) {
        if ([NSDate judgeTimeWithStartTime:StartTime1 expireTime:ExpireTime1] || [NSDate judgeTimeWithStartTime:StartTime2 expireTime:ExpireTime2]) {
            self.dk_manager.themeVersion = DKThemeVersionNight;
        } else {
            self.dk_manager.themeVersion = DKThemeVersionNormal;
        }
    }
    
#pragma mark - 重返系统事件
    [self swiz_viewWillAppear:animated];
}

#pragma mark - 响应自定义viewDidLoad事件
- (void)swiz_viewDidLoad:(BOOL)animated
{
#pragma mark - 处理返回按钮显示/隐藏
    self.navigationItem.hidesBackButton = ([self respondsToSelector:@selector(isHideBackButton)] && [self isHideBackButton] == YES) ? YES : NO;
    
#pragma mark - 处理返回按钮标题显示/隐藏
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                             initWithTitle:@""
                                             style:UIBarButtonItemStylePlain
                                             target:nil
                                             action:nil];
#pragma mark - 重返系统事件
    [self swiz_viewDidLoad:animated];
}

#pragma mark - 响应自定义viewDidAppear:事件
- (void)swiz_viewDidAppear:(BOOL)animated
{
#pragma mark - 处理侧滑
    // 默认打开侧滑
    BOOL enabled = YES;
    if (self.navigationController.viewControllers.count == 1) {
        // 只有一个控制器的时候禁止侧滑,防止页面卡死
        enabled = NO;
    } else {
        if ([self respondsToSelector:@selector(enableInteractivePopGestureRecognizer)] && [self enableInteractivePopGestureRecognizer] == NO) {
            enabled = NO;
        }
    }
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = enabled;
    }
    
#pragma mark - 页面浏览统计
    
#pragma mark - 重返系统事件
    [self swiz_viewDidAppear:animated];
}

@end