0

I want to swizzle URLSession class method data(for:) but not been able to get it done.

I tried this swizzling in Swift but it not working, bellow is the error:

the code: #selector(URLSession.data(for:))

the error: enter image description here

Please help me.


Update 1:

My Problem:

I have swizzle URLSession with

#selector(URLSession.dataTask(with:) as (URLSession) -> (URLRequest) -> URLSessionDataTask)

and

#selector(URLSession.dataTask(with:) as (URLSession) -> (URL) -> URLSessionDataTask).

It works fine before iOS 16+ with the follow code: try await URLSession.shared.data(for: request). I don't know why, So, I want swizzle the data(for:) method to test my guess, but it fails.

cnbleu
  • 63
  • 1
  • 9
  • Sounds like an [XY problem](http://xyproblem.info/). Why are you trying to do this? – Sweeper Nov 25 '22 at 08:52
  • Isn't the real method `data(for:delegate:)` with default parameter to `delegate` as `nil`? But since it might need the async/await, which I'm not sure is exposed to Objective-C, maybe the old version with the completionHandler parameter? – Larme Nov 25 '22 at 09:00
  • @Sweeper Because of the URLSession swizzle does not work on iOS 16+ version when call ```URLSession.data(for: request)``` mehtod. – cnbleu Nov 25 '22 at 09:03
  • @Larme ```data(for:delegate:)``` was introduced into from iOS 15+, even use ```data(for:delegate:)``` that will fail – cnbleu Nov 25 '22 at 09:06
  • @Larme yes, it should need the async/await, and it works well on iOS 15.5, not iOS 16+. So, I want to swizzle the ```data(for:)``` for resovle this issue. – cnbleu Nov 25 '22 at 09:08

0 Answers0