0

UIApplication メソッドにスウィズリング メソッドを使用しようとする以下のコードがあります。しかし、メソッドが呼び出されません。その間、このコードをフレームワークまたはプライベート ポッドに追加しようとしています。

extension UIApplication {
public override class func initialize() {
    struct Static {
        static var token: dispatch_once_t = 0
    }

    struct SwizzlingSelector {
        let original:Selector
        let swizzled:Selector
    }

    // make sure this isn't a subclass
    if self !== UIApplication.self {
        return
    }

    dispatch_once(&Static.token) {
        let selectors = [
            SwizzlingSelector(
                original: Selector("application:didFinishLaunchingWithOptions:"),
                swizzled: Selector("custome_application:didFinishLaunchingWithOptions:")
            ),
            SwizzlingSelector(
                original: Selector("applicationDidEnterBackground:"),
                swizzled: Selector("custom_applicationDidEnterBackground:")
            )
        ]

        for selector in selectors {
            let originalMethod = class_getInstanceMethod(self, selector.original)
            let swizzledMethod = class_getInstanceMethod(self, selector.swizzled)

            let didAddMethod = class_addMethod(self, selector.original, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

            if didAddMethod {
                class_replaceMethod(self, selector.swizzled, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        }
    }
}

// MARK: - Method Swizzling
public func custome_application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    print("is here")
    return true
}

func custom_applicationDidEnterBackground(application: UIApplication) {
    print("is herer")
}
}
4

1 に答える 1

0

UIApplication の拡張としてではなく、AppDelegate にこのスウィズリング メソッドを実装する必要があると思います。AppDelegate のメソッドをスウィズルしますが、UIApplication のメソッドはスウィズルしません。

extension YourAppDelegate {
public override class func initialize() {
    struct Static {
        static var token: dispatch_once_t = 0
    }

    struct SwizzlingSelector {
        let original:Selector
        let swizzled:Selector
    }

    // make sure this isn't a subclass
    if self !== YourAppDelegate.self {
        return
    }

    dispatch_once(&Static.token) {
        let selectors = [
            SwizzlingSelector(
                original: Selector("application:didFinishLaunchingWithOptions:"),
                swizzled: Selector("custome_application:didFinishLaunchingWithOptions:")
            ),
            SwizzlingSelector(
                original: Selector("applicationDidEnterBackground:"),
                swizzled: Selector("custom_applicationDidEnterBackground:")
            )
        ]

        for selector in selectors {
            let originalMethod = class_getInstanceMethod(self, selector.original)
            let swizzledMethod = class_getInstanceMethod(self, selector.swizzled)

            let didAddMethod = class_addMethod(self, selector.original, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

            if didAddMethod {
                class_replaceMethod(self, selector.swizzled, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        }
    }
}

// MARK: - Method Swizzling
public func custome_application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    print("is here")
    return true
}

func custom_applicationDidEnterBackground(application: UIApplication) {
    print("is herer")
}
}
于 2016-06-27T09:49:42.737 に答える