How to swizzle NSURLSession class method dataTaskWithUrl

无人久伴 提交于 2019-12-02 17:37:11

问题


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 !!


回答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




回答2:


// 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)
}
}


来源:https://stackoverflow.com/questions/35570809/how-to-swizzle-nsurlsession-class-method-datataskwithurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!