diff --git a/Sources/NetworkInjector.swift b/Sources/NetworkInjector.swift index 69bf89d..6c0170d 100644 --- a/Sources/NetworkInjector.swift +++ b/Sources/NetworkInjector.swift @@ -84,11 +84,9 @@ extension NetworkInjector { private func injectAllURLConnection() { // Find all classes that conform URLConnection delegates and start the injection - let allClasses = Runtime.getAllClasses() + let allClasses = Runtime.getAllClassesConformsProtocol(NSURLConnectionDataDelegate.self) for anyClass in allClasses { - if class_conformsToProtocol(anyClass, NSURLConnectionDataDelegate.self) { - injectURLConnectionDelegate(anyClass: anyClass) - } + injectURLConnectionDelegate(anyClass: anyClass) } } diff --git a/Sources/Runtime.swift b/Sources/Runtime.swift index e72c698..79f02f7 100644 --- a/Sources/Runtime.swift +++ b/Sources/Runtime.swift @@ -10,34 +10,19 @@ import Foundation struct Runtime { - static func getAllClasses() -> [AnyClass] { - let expectedClassCount = objc_getClassList(nil, 0) - let allClasses = UnsafeMutablePointer.allocate(capacity: Int(expectedClassCount)) - - let autoreleasingAllClasses = AutoreleasingUnsafeMutablePointer(allClasses) - let actualClassCount: Int32 = objc_getClassList(autoreleasingAllClasses, expectedClassCount) - - var classes = [AnyClass]() - for i in 0 ..< actualClassCount { - if let currentClass: AnyClass = allClasses[Int(i)] { - classes.append(currentClass) + static func getAllClassesConformsProtocol(_ aProtocol: Protocol) -> [AnyClass] { + var numberClasses: UInt32 = 0 + var result = Array() + if let classes = UnsafePointer(objc_copyClassList(&numberClasses)) { + for i in 0.. [String] { - var methods: [String] = [] - var methodCount: UInt32 = 0 - let methodList = class_copyMethodList(anyClass, &methodCount) - for i in 0 ..< Int(methodCount) { - let selName = sel_getName(method_getName(methodList![i])) - let name = String(cString: selName) - methods.append(name) - } - return methods + return result } }