KMP项目iOS端报错‘TLS sessions are not supported on Native platform’求助
KMP(原KMM)iOS端调用HTTPS接口报错:"TLS sessions are not supported on Native platform" 解决方案
问题背景
在KMP跨平台项目中,Android端调用HTTPS接口正常,但iOS端触发报错:TLS sessions are not supported on Native platform。尝试过将HTTPS改为HTTP(安全性不足)、修改iOS的info.plist配置ATS规则,均未解决问题。
通用层API请求代码
class Greeting { suspend fun sendLoginRequest(email: String, password: String):ResponseResult { return try { val client = HttpClient(CIO) val response: HttpResponse = client.submitForm( url = "https://jsonplaceholder.typicode.com/posts/1/comments", formParameters = parameters { append("email", email) append("password", password) } ) val responseBody = response.bodyAsText() ResponseResult(responseBody, null) } catch (e: Exception) { ResponseResult(null, e.message ?: "An unknown error occurred.") } } }
Gradle依赖配置
listOf( iosX64(), iosArm64(), iosSimulatorArm64() ).forEach { it.binaries.framework { baseName = "shared" } } val ktorVersion = "2.3.4" sourceSets { val commonMain by getting { dependencies { implementation("io.ktor:ktor-client-core:$ktorVersion") implementation("io.ktor:ktor-client-cio:$ktorVersion") } } val androidMain by getting { dependencies{ implementation("io.ktor:ktor-client-okhttp:$ktorVersion") implementation("io.ktor:ktor-client-core:$ktorVersion") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.2") } } val iosX64Main by getting val iosArm64Main by getting val iosSimulatorArm64Main by getting val iosMain by getting { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) iosSimulatorArm64Main.dependsOn(this) dependencies { implementation("io.ktor:ktor-client-darwin:$ktorVersion") } } val commonTest by getting { dependencies { implementation(kotlin("test")) } } }
问题根源
核心是平台客户端混用:
- 通用层
commonMain引入了ktor-client-cio,代码直接实例化HttpClient(CIO) - iOS层
iosMain虽引入苹果平台专属的ktor-client-darwin,但代码未使用该客户端 - CIO客户端在iOS Native平台不支持TLS会话机制,导致HTTPS请求失败
解决方案
1. 调整依赖配置
将CIO客户端依赖从通用层移到Android层,通用层仅保留核心依赖:
sourceSets { val commonMain by getting { dependencies { // 仅保留核心依赖,移除CIO implementation("io.ktor:ktor-client-core:$ktorVersion") } } val androidMain by getting { dependencies{ // Android端使用CIO或OkHttp均可 implementation("io.ktor:ktor-client-cio:$ktorVersion") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.2") } } val iosMain by getting { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) iosSimulatorArm64Main.dependsOn(this) dependencies { // iOS端保留Darwin客户端 implementation("io.ktor:ktor-client-darwin:$ktorVersion") } } }
2. 用Expect/Actual实现平台特定客户端初始化
在通用层定义客户端创建预期函数,各平台实现对应客户端:
通用层(commonMain)
expect fun createHttpClient(): HttpClient
Android层(androidMain)
actual fun createHttpClient(): HttpClient { return HttpClient(CIO) { // 可添加Android端专属配置,比如超时、日志 install(HttpTimeout) { requestTimeoutMillis = 10000 } } }
iOS层(iosMain)
actual fun createHttpClient(): HttpClient { return HttpClient(Darwin) { // iOS端专属配置 install(HttpTimeout) { requestTimeoutMillis = 10000 } } }
3. 修改API请求代码
替换直接实例化CIO的代码,使用平台适配的客户端:
class Greeting { suspend fun sendLoginRequest(email: String, password: String): ResponseResult { return try { val client = createHttpClient() // 使用平台适配的客户端 val response: HttpResponse = client.submitForm( url = "https://jsonplaceholder.typicode.com/posts/1/comments", formParameters = parameters { append("email", email) append("password", password) } ) val responseBody = response.bodyAsText() ResponseResult(responseBody, null) } catch (e: Exception) { ResponseResult(null, e.message ?: "An unknown error occurred.") } } }
4. 清理不必要的配置
删除之前添加的info.plist中的ATS例外配置,Darwin客户端支持标准HTTPS请求,默认ATS规则即可满足安全要求。
验证
修改后重新构建项目,iOS端即可正常发起HTTPS请求,无需降低安全性使用HTTP。
内容的提问来源于stack exchange,提问作者Abdulrahman Hasanato
相关产品推荐
相关产品推荐

