https://kotlinlang.org logo
Title
h

Hien Nguyen

07/14/2022, 10:21 AM
I need set NSURLSessionConfiguration into IosClientEngineConfig, how to do it ?
t

Trương Anh Ngoc

07/14/2022, 10:33 AM
I’m having the same issue, need to set
NSURLSessionConfiguration
into
IosClientEngineConfig
but don’t know how to set. I’m using
ktor-client-iOS
1.6.7
a

Aleksei Tirman [JB]

07/14/2022, 10:58 AM
You can use the
configureSession
method to mutate an
NSURLSessionConfiguration
:
val client = HttpClient(Darwin) { // `Ios` for Ktor 1.6.7
    engine {
        configureSession { // this: NSURLSessionConfiguration
            // mutate configuration here
        }
    }
}
👍 1
t

Trương Anh Ngoc

07/14/2022, 4:05 PM
Hi @Aleksei Tirman [JB] Thanks for your suggestion. Currently in my situation, I want to inject a
NSURLSessionConfiguration
from native into
HttpClient(Darwin)
Ex: • On Swift code, I have a
URLSessionConfiguration.default
& want to pass it to KMM • On KMM code, how can we config for this case with
HttpClient(Darwin)
val client =  HttpClient(Darwin) {
        config(this)
        engine {
            configureSession {  
                // How can I inject NSURLSessionConfiguration from native here
            }
        }
    }
a

Aleksei Tirman [JB]

07/14/2022, 5:50 PM
You can find an information about using Swift library from Kotlin code here. Unfortunately, there is no way to pass a configuration object and you can only mutate the one that is created by Ktor.
In Ktor 2.1.0 (will be released soon) you can use preconfigured session object like so:
val config = NSURLSessionConfiguration.defaultSessionConfiguration()
val session = NSURLSession.sessionWithConfiguration(config)

val client = HttpClient(Darwin) {
    engine {
        usePreconfiguredSession(session)
    }
}
t

Trương Anh Ngoc

07/15/2022, 12:56 AM
It’s really helpful information. Thanks for your quickly response. I will wait for
2.1.0
version to update it.
CC @phong le