I have created NSURLConnection method swizzling for sendSynchronousRequest, however its not been working below is my code. Whenever I am trying to call this from main function, its crashing.
let originalRequestSyncSelector = #selector(self.sendSynchronousRequest(_:returning:))
let swizzledRequestSyncSelector = #selector(self.swizzSendSynchronousRequest(_:returning:error:))
let originalSyncRequestMethod = class_getClassMethod(self, originalRequestSyncSelector)
let swizzledSyncRequestMethod = class_getInstanceMethod(self, swizzledRequestSyncSelector)
if originalSyncRequestMethod == nil || swizzledSyncRequestMethod == nil {
return
}
method_exchangeImplementations(originalSyncRequestMethod!, swizzledSyncRequestMethod!)
@objc func swizzSendSynchronousRequest(_ request: NSURLRequest?, returning response: URLResponse?, error: Error?) -> Data? {
var tempData: Data?
print("Inside Sync Swizzled Method")
print("------------\(String(describing: request))")
return tempData
}
There's a couple of things that might cause problems:
errorparameter, but is a throwing function. Also, unlike your swizzled method,sendSynchronousRequesttakes aURLRequestinstead of anNSURLRequest.sendSynchronousRequestis exposed to Swift as a throwing function. It works if your swizzled function is not marked withthrows, but throwing functions can't be exposed to ObjC and might cause issues when being swizzled.Here is some working code for a playground:
Regardless, I think it would be a better idea to perform swizzling in Objective-C. There's much better documentation on how to do that, and you can avoid pitfalls in Swift <-> Objective-C bridging magic.