【问题标题】:How to swizzle NSURLSession class method dataTaskWithUrl如何调配 NSURLSession 类方法 dataTaskWithUrl
【发布时间】:2016-02-23 06:59:07
【问题描述】:

我一直在尝试调整 NSURLSession 类方法 dataTaskWithRequest 但无法完成

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

提前致谢!!

【问题讨论】:

标签: ios swift swizzling method-swizzling


【解决方案1】:

我必须以 JSON(或 NSDictionary )格式将我的数据上传到服务器。
我做过这样的事情...

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()


更多或相关答案请访问链接
How to swizzle in private method

希望对你有帮助

【讨论】:

  • 这个问题被标记为swift。为什么要在 Objective-C 中提供答案?
  • 我认为它可能会让您知道...如何解决您的问题。我从以前的项目中准备好了代码/解决方案。您可以转换为 swift。
  • 你好。该问题被标记为“swift”,请以 Swift 而非其他语言提供答案。谢谢!
  • 嘿@EricD。这是快速代码。试试吧,它可能对你有帮助!
  • 我问过如何调整 dataTaskWithURL 方法而不是如何使用它。如果您对此有解决方案,请提供@AvinashJadhav
【解决方案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)
}
}

【讨论】:

  • 这对我不起作用,因为您在回答中没有做任何不同的事情,只是改变了编写选择器的方式@Andey
  • 我想在 URLSession 中调配方法但无法实现。你能帮帮我吗?
  • 你们中的任何人都完成了 URLSession 中的方法,如果完成请帮助我@ManuGupta
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多