0

I have been trying to swizzle NSURLSession class method dataTaskWithRequest but not been able to get it done

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

    if self !== NSURLSession.self {
        return
    }

    dispatch_once(&Static.token) {
        let originalSelector = Selector("dataTaskWithRequest:completionHandler:")
        let swizzledSelector = Selector("my_dataTaskWithRequest:completionHandler:")

        let originalMethod = class_getInstanceMethod(self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

        let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

        if didAddMethod {
            class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

// Swizzled Method
func my_dataTaskWithRequest(request: NSURLRequest,completionHandler: (NSData?, NSURLResponse?, NSError?)) -> NSURLSessionDataTask {

    print("Inside Swizzled Method")

    return my_dataTaskWithRequest(request,completionHandler: completionHandler)
}
}

Thanks in advance !!

Manu Gupta
  • 1,759
  • 1
  • 14
  • 22
  • Are you sure your `initialize` is actually called? Anyway, you should implement `load`, not override `initialize`. [See this question for details.](http://stackoverflow.com/questions/13326435/nsobject-load-and-initialize-what-do-they-do). – DarkDust Feb 26 '16 at 14:38

2 Answers2

-1

I had to upload my data in JSON(or NSDictionary )format to server.
I have done something like this...

let urlPath:String = apiURL + apiVersion + url + "?api_key=" + apiKey
//  OR
let urlPath:String = "your url string"
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()

let task = session.dataTaskWithURL(url!, completionHandler: {(data, reponse, error) in
println("Task completed")
// rest of the function...
})

task.resume()


For more or relative answer visit link
How to swizzle in private method

I hope it may help you

Community
  • 1
  • 1
Avinash Jadhav
  • 491
  • 4
  • 17
  • 1
    This question is tagged [tag:swift]. Why would you provide an answer in Objective-C? – JAL Feb 25 '16 at 14:41
  • I think it may give you idea... how to resolve your problem. I had ready code/solution from my previous project. You can convert to swift. – Avinash Jadhav Feb 25 '16 at 14:55
  • Hello. The question is tagged "swift", please provide an answer in Swift, not in another language. Thanks! – Eric Aya Feb 26 '16 at 13:19
  • 1
    Hey @EricD. This is swift code. Try it, it may help you! – Avinash Jadhav Feb 26 '16 at 13:45
  • I have asked how to swizzle the dataTaskWithURL method not how to use it. Please provide if you have solution for that @AvinashJadhav – Manu Gupta Feb 29 '16 at 05:25
  • Dear @ManuGupta I was provided you with my solution that was worked for me successfully, but you couldn't understood. Now solution changed. – Avinash Jadhav Feb 29 '16 at 11:39
-1
// i hope it may be help you
  extension NSURLSession{
  public override class func initialize() {
   struct Static {
      static var token: dispatch_once_t = 0
   }

  if self !== NSURLSession.self {
      return
  }

  dispatch_once(&Static.token) {
 let method1: Method = class_getInstanceMethod(self,Selector("dataTaskWithRequest:completionHandler:"));
 let method2 = class_getInstanceMethod(self, Selector("my_dataTaskWithRequest:completionHandler:"));
  method_exchangeImplementations(method1, method2);
}
}
// Swizzled Method
func my_dataTaskWithRequest(request: NSURLRequest,completionHandler: (NSData?, NSURLResponse?, NSError?)) -> NSURLSessionDataTask {

print("Inside Swizzled Method")

return my_dataTaskWithRequest(request,completionHandler: completionHandler)
}
}
Satyanarayana
  • 1,059
  • 6
  • 16