Skip to content

Instantly share code, notes, and snippets.

@CognitiveDisson
Last active January 2, 2019 13:58
Show Gist options
  • Save CognitiveDisson/8bea14e2578a213478f0d6d3f8d30c07 to your computer and use it in GitHub Desktop.
Save CognitiveDisson/8bea14e2578a213478f0d6d3f8d30c07 to your computer and use it in GitHub Desktop.
//
// AppDelegateHandler.swift
// Pods
//
// Created by Andrey Zarembo on 10.02.16.
//
//
import Foundation
public class AppDelegateHandler: NSObject, UIApplicationDelegate {
}
//
// AppDelegateProxy.swift
// Pods
//
// Created by Andrey Zarembo on 10.02.16.
//
//
import Foundation
import UIKit
public class AppDelegateProxy: NSObject, UIApplicationDelegate {
public var handlers:[AppDelegateHandler] = []
public override func respondsToSelector(aSelector: Selector) -> Bool {
if self.shouldForwardSelector(aSelector) {
for handler in self.handlers {
if handler.respondsToSelector(aSelector) {
return true
}
}
}
return NSObject.instancesRespondToSelector(aSelector)
}
public override func forwardingTargetForSelector(aSelector: Selector) -> AnyObject? {
for handler in self.handlers {
if handler.respondsToSelector(aSelector) {
return handler
}
}
return nil
}
private func shouldForwardSelector(aSelector: Selector) -> Bool {
return isSelector(aSelector, fromProtocol: UIApplicationDelegate.self) &&
!isSelector(aSelector, fromProtocol: NSObjectProtocol.self)
}
private func isSelector(aSelector: Selector, fromProtocol aProtocol:Protocol) -> Bool {
let requiredMethod = [false,false,true,true]
let instanceMethod = [false,true,false,true]
for callVariant in 0..<4 {
let methodDescription = protocol_getMethodDescription(aProtocol,
aSelector,
requiredMethod[callVariant],
instanceMethod[callVariant])
if methodDescription.types != nil && methodDescription.name != nil {
return true
}
}
return false;
}
}
@Maxatma
Copy link

Maxatma commented Jan 2, 2019

swift 4.2 version:

//
//  AppDelegateProxy.swift
//  Pods
//
//  Created by Andrey Zarembo on 10.02.16.
//
//

import UIKit

public class AppDelegateProxy: NSObject, UIApplicationDelegate {
	public var handlers: [AppDelegateHandler] = []

	public override func responds(to aSelector: Selector!) -> Bool {
		if shouldForwardSelector(aSelector: aSelector) {
			for handler in handlers {
				if handler.responds(to: aSelector) {
					return true
				}
			}
		}
		return NSObject.instancesRespond(to: aSelector)
	}

	public override func forwardingTarget(for aSelector: Selector!) -> Any? {
		for handler in handlers {
			if handler.responds(to: aSelector) {
				return handler
			}
		}
		return nil
	}

	private func shouldForwardSelector(aSelector: Selector) -> Bool {
		return
			isSelector(aSelector: aSelector, fromProtocol: UIApplicationDelegate.self) &&
			!isSelector(aSelector: aSelector, fromProtocol: NSObjectProtocol.self)
	}

	private func isSelector(aSelector: Selector, fromProtocol aProtocol: Protocol) -> Bool {
		let requiredMethod = [false, false, true, true]
		let instanceMethod = [false, true, false, true]

		for callVariant in 0 ..< 4 {
			let methodDescription = protocol_getMethodDescription(aProtocol,
			                                                      aSelector,
			                                                      requiredMethod[callVariant],
			                                                      instanceMethod[callVariant])
			if methodDescription.types != nil, methodDescription.name != nil {
				return true
			}
		}

		return false
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment