如何调配NSURLSession类方法dataTaskWithUrl [英] How to swizzle NSURLSession class method dataTaskWithUrl
本文介绍了如何调配NSURLSession类方法dataTaskWithUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试调用NSURLSession类方法dataTaskWithRequest但是无法完成它
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)
}
}
提前致谢!!
推荐答案
我必须将我的数据以JSON(或NSDictionary)格式上传到服务器。
我做过类似的事情...
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()
如需更多或相对答案,请访问链接
如何以私密方式调动
我希望它可以帮到你
这篇关于如何调配NSURLSession类方法dataTaskWithUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文