如何使用Ktor中的通用引擎

发布于 2025-01-20 23:50:46 字数 301 浏览 4 评论 0 原文

嘿,我正在尝试在现有的Android和iOS项目中实现KMM。我关注此 kmm-config 。我想实现拦截器和身份验证。在DOC示例中,Android和iOS KTOR发动机分别在Androidmain和Iosmain中分开。因此,我想在两个平台上编写拦截器,例如单独的或仅在Commmain中使用。是否有一种在两个平台中使用通用引擎的方法?谢谢

Hey I am trying to implement KMM in my existing Android and iOS project. I following this kmm-config. I want to implement interceptor and authentication. In doc example both Android and iOS ktor engine is separated in androidMain and iosMain respectively. So I want to write interceptor in both platform like separate or just use in commonMain. Is there is a way to use common engine in both platform? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2025-01-27 23:50:46

HTTP引擎是平台依赖性的,因此您必须为每个平台使用不同的引擎,您可以在每个平台的共享软件包中定义这些引擎。

使用期望关键字创建一个变量, ComponMain

expect val httpEngine: HttpEngine

在每个平台内提供实际声明 main main package

android

actual val httpEngine = Android.create {
    //configure intercepter here
}

ios ios,

acutal val httpEngine = Ios.create {
    //configure intercepter here
}

您可以使用此 httpengine commonmain 软件包创建 httpclient 和配置身份验证,

fun createHttpClient(httpEngine: HttpClientEngine) =
    HttpClient(httpEngine) {
        install(Auth) {
            //configure authentication
        }
    }
    

您可以阅读有关 ktor authentication 在这里

HTTP Engines are platform-dependent, so you have to use different engines for each platform, which you can define in each platform's shared package.

Create a variable with expect keyword, inside commonMain

expect val httpEngine: HttpEngine

Provide an actual declaration inside each platform Main package

Android

actual val httpEngine = Android.create {
    //configure intercepter here
}

IOS

acutal val httpEngine = Ios.create {
    //configure intercepter here
}

You can use this httpEngine inside commonMain package to create an HttpClient and configure authentication

fun createHttpClient(httpEngine: HttpClientEngine) =
    HttpClient(httpEngine) {
        install(Auth) {
            //configure authentication
        }
    }
    

You can read more about ktor authentication here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文