美文网首页
swiftlint使用 & swift替换AppIcon & S

swiftlint使用 & swift替换AppIcon & S

作者: 呦释原点 | 来源:发表于2022-02-07 16:41 被阅读0次
    swiftlint使用参考

    https://www.jianshu.com/p/aaadbe445a3a
    https://www.jianshu.com/p/a1afc52ec0ff

    iOS替换AppIcon 参考

    https://www.jianshu.com/p/86cd1251049c

    Swift开发替换方法

    1、OC开发的时候再+load中替换方法。swift开发没有load方法了,替换方法实现方式有两种
    1.1使用OC
    创建OC拓展文件, 将文件引入到混合开发桥接文件中

    // 去掉切换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方式替换方法
    创建分类添加公共类方法实现替换
    例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)
    
            //在进行 Swizzling 的时候,需要用 class_addMethod 先进行判断一下原有类中是否有要替换方法的实现
            let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
            //如果 class_addMethod 返回 yes,说明当前类中没有要替换方法的实现,所以需要在父类中查找,这时候就用到 method_getImplemetation 去获取 class_getInstanceMethod 里面的方法实现,然后再进行 class_replaceMethod 来实现 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)
    
            //在进行 Swizzling 的时候,需要用 class_addMethod 先进行判断一下原有类中是否有要替换方法的实现
            let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
            //如果 class_addMethod 返回 yes,说明当前类中没有要替换方法的实现,所以需要在父类中查找,这时候就用到 method_getImplemetation 去获取 class_getInstanceMethod 里面的方法实现,然后再进行 class_replaceMethod 来实现 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 方法中调用这个公开的类方法

    // 替换原来的方法,以正确方式显示图片数量
    PhotoBrowserController.initializeMethod()
    UIViewController.initializeMethod2()
    

    相关文章

      网友评论

          本文标题:swiftlint使用 & swift替换AppIcon & S

          本文链接:https://www.haomeiwen.com/subject/prhztrtx.html