https://kotlinlang.org logo
#ktor
Title
# ktor
p

phong le

03/02/2023, 2:34 AM
Hi team, hope you are doing well From the iOS native, I need to set delegate for NSURLSession. So is there any way to pass NSURLSession or NSURLSessionDelegate back to iOS? Any of your suggestions will make my day. Thanks. cc @Hien Nguyen
🙌 1
a

Aleksei Tirman [JB]

03/02/2023, 6:39 AM
You can use the
usePreconfiguredSession
method inside the
engine
block. Here is an example:
Copy code
val client = HttpClient(Darwin) {
    val delegate = KtorNSURLSessionDelegate()
    val session = NSURLSession.sessionWithConfiguration(
        NSURLSessionConfiguration.defaultSessionConfiguration(),
        delegate,
        delegateQueue = NSOperationQueue()
    )
    engine {
        usePreconfiguredSession(session, delegate)
    }
}
p

phong le

03/02/2023, 8:34 AM
Thanks @Aleksei Tirman [JB] Currently, we used ktor version
2.1.0
, also using
handleChallenge
AND
configureSession
in HttpClient(Darwin). Following by new version ktor https://api.ktor.io/ktor-client/ktor-client-darwin/io.ktor.client.engine.darwin/-darwin-client-engine-config/use-preconfigured-session.html
Copy code
"Set a session to be used to make HTTP requests. If the preconfigured session is set, configureSession and handleChallenge blocks will be ignored."
Do you have any suggestions? cc @Hien Nguyen @Trương Anh Ngoc
👍 1
hi @Aleksei Tirman [JB] We used CertificatePinner.Builder(), configureSession and handleChallenge for config http. Under your suggestion, using new NSURLSession and usePreconfiguredSession will ignore our previous config NSURLSession. Do you have any ideas please feel free to share. Thanks cc @Hien Nguyen @Trương Anh Ngoc
cc @Stoney
❤️ 1
cc @Hung Luong
❤️ 1
a

Aleksei Tirman [JB]

03/13/2023, 12:28 PM
The problem is that the primary constructor of the
KtorNSURLSessionDelegate
is internal so there is no way to pass the
ChallengeHandler
to it.
Would it solve your problem if the constructor were public?
p

phong le

03/16/2023, 3:23 AM
yup, the sound is good, then we can set KtorNSURLSessionDelegate from the out side, or we can have a new func like configureSession, something like that
Copy code
public fun configureSessionDelegate(block: NSURLSessionDelegate.() -> Unit)
How about your ideals @Aleksei Tirman [JB]? cc @Hien Nguyen @Trương Anh Ngoc @Hung Luong @Stoney
a

Aleksei Tirman [JB]

03/16/2023, 7:01 AM
I don’t have a specific preference. Can you please file an issue then?
p

phong le

03/21/2023, 3:13 AM
Please kindly refer to this issue. Thanks, so excited to see it available on version 2.2.4 https://youtrack.jetbrains.com/issue/KTOR-5688/KTORiOS-Support-access-NSURLSessionDelegate
a

Aleksei Tirman [JB]

05/31/2023, 5:42 AM
I'll ask the assigned developer.
❤️ 1
61 Views