swiftlint使用 & swift替換AppIcon & Swift替換方法

swiftlint使用參考

http://www.itdecent.cn/p/aaadbe445a3a
http://www.itdecent.cn/p/a1afc52ec0ff

iOS替換AppIcon 參考

http://www.itdecent.cn/p/86cd1251049c

Swift開發(fā)替換方法

1、OC開發(fā)的時候再+load中替換方法。swift開發(fā)沒有l(wèi)oad方法了,替換方法實(shí)現(xiàn)方式有兩種
1.1使用OC
創(chuàng)建OC拓展文件, 將文件引入到混合開發(fā)橋接文件中

// 去掉切換AppIcon的彈框, 這里用的別人的代碼。 文件名稱是 UIViewController+YJPresent_NoAlert.h
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (XZPresent_NoAlert)

@end

NS_ASSUME_NONNULL_END


// UIViewController+XZPresent_NoAlert.m

#import <objc/runtime.h>

@implementation UIViewController (XZPresent_NoAlert)

+ (void)load {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
        Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(xz_presentViewController:animated:completion:));
        
        method_exchangeImplementations(presentM, presentSwizzlingM);
    });
}

- (void)xz_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    
    if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
        UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
        if (alertController.title == nil && alertController.message == nil) {
            return;
        }
    }
    
    [self xz_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end

在橋接文件中引入

#import "UIViewController+YJPresent_NoAlert.h"

1.2使用swift方式替換方法
創(chuàng)建分類添加公共類方法實(shí)現(xiàn)替換
例1:

extension PhotoBrowserController {
    public class func initializeMethod(){
        let originalSelector = #selector(PhotoBrowserController.updateNavigation)
        let swizzledSelector = #selector(PhotoBrowserController.switchUpdateNavigation)

        let originalMethod = class_getInstanceMethod(self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

        //在進(jìn)行 Swizzling 的時候,需要用 class_addMethod 先進(jìn)行判斷一下原有類中是否有要替換方法的實(shí)現(xiàn)
        let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
        //如果 class_addMethod 返回 yes,說明當(dāng)前類中沒有要替換方法的實(shí)現(xiàn),所以需要在父類中查找,這時候就用到 method_getImplemetation 去獲取 class_getInstanceMethod 里面的方法實(shí)現(xiàn),然后再進(jìn)行 class_replaceMethod 來實(shí)現(xiàn) Swizzing
        if didAddMethod {
            class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
        } else {
            method_exchangeImplementations(originalMethod!, swizzledMethod!)
        }
    }

    @objc func switchUpdateNavigation() {
        switchUpdateNavigation()
        
        if let t = super.title {
            let arr = t.split(separator: " ")
            self.pbNumLb.text = String(arr[0])+"/"+String(arr[2])
        }
    }
}

例2:

// 替換AppIcon禁止彈框
extension UIViewController {
    public class func initializeMethod2(){
        
        let originalSelector = #selector(UIViewController.yj_present(viewControllerToPresent:animated:completion:))
        let swizzledSelector = #selector(UIViewController.present(_:animated:completion:))

        let originalMethod = class_getInstanceMethod(self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

        //在進(jìn)行 Swizzling 的時候,需要用 class_addMethod 先進(jìn)行判斷一下原有類中是否有要替換方法的實(shí)現(xiàn)
        let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
        //如果 class_addMethod 返回 yes,說明當(dāng)前類中沒有要替換方法的實(shí)現(xiàn),所以需要在父類中查找,這時候就用到 method_getImplemetation 去獲取 class_getInstanceMethod 里面的方法實(shí)現(xiàn),然后再進(jìn)行 class_replaceMethod 來實(shí)現(xiàn) Swizzing
        if didAddMethod {
            class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
        } else {
            method_exchangeImplementations(originalMethod!, swizzledMethod!)
        }
    }

    @objc func yj_present(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
        if let alertC = viewControllerToPresent as? UIAlertController {
            if alertC.title == nil && alertC.message == nil {
                return
            }
        }
        self.yj_present(viewControllerToPresent: viewControllerToPresent, animated: animated, completion: completion)
    }
}

使用:
在AppDelegate.swift文件中的 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 方法中調(diào)用這個公開的類方法

// 替換原來的方法,以正確方式顯示圖片數(shù)量
PhotoBrowserController.initializeMethod()
UIViewController.initializeMethod2()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容