0Pricing
Kotlin Multiplatform Academy · درس

إضافة Ktor واختيار محركات المنصة

ربط محركَي OkHttp وDarwin لكل هدف

إضافة Ktor واختيار محركات المنصة درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

One HTTP Client Everywhere

Ktor Client is a multiplatform HTTP library, so you write networking once in commonMain and it runs on Android and iOS alike.

Core Plus Engine

Ktor splits into a shared core and per-platform engines. The core gives the API, the engine does the actual socket work.

Add the Core Dependency

In your shared module you add ktor-client-core to commonMain so every target can build requests with the same API.

implementation("io.ktor:ktor-client-core:2.3.12")

Android Wants OkHttp

For Android you pick the OkHttp engine, a battle-tested JVM client that Ktor wraps cleanly behind its shared API.

implementation("io.ktor:ktor-client-okhttp:2.3.12")

iOS Wants Darwin

For iOS you pick the Darwin engine, which runs on Apple's native URLSession under the hood for true platform networking.

implementation("io.ktor:ktor-client-darwin:2.3.12")

Engines Live in Source Sets

Each engine belongs in its own source set: OkHttp in androidMain and Darwin in iosMain, never in commonMain.

androidMain.dependencies { implementation(okhttp) }

Create the Client Once

In commonMain you create an HttpClient with no engine argument, and Ktor auto-selects the one bundled for that platform.

val client = HttpClient()

Auto Engine Selection

Because only one engine ships per target, Ktor's auto-selection finds it. Your shared code stays free of any platform import.

Or Pass an Engine

You can also pass an engine explicitly, which is handy when you want per-platform config like timeouts or proxies.

val client = HttpClient(OkHttp)

Close When Done

The client holds real resources, so call close when you no longer need it to release connections cleanly.

client.close()

Why Split This Way

This core-plus-engine design lets Ktor reuse each OS's best networking stack while you keep one shared API for all of them.

Quick Check

Which engine should an iOS target use?

Recap

Ktor shares a core API but needs an engine per platform: OkHttp for Android, Darwin for iOS. Auto-selection keeps shared code clean.

الأسئلة الشائعة

هل درس «إضافة Ktor واختيار محركات المنصة» مجاني؟

نعم — نص درس «إضافة Ktor واختيار محركات المنصة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «إضافة Ktor واختيار محركات المنصة»؟

ربط محركَي OkHttp وDarwin لكل هدف تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «إضافة Ktor واختيار محركات المنصة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إضافة Ktor واختيار محركات المنصة
  2. أول طلب GET لكم
  3. POST والعناوين ومعلمات الاستعلام
  4. المهلات والسجلات وعناوين Base URLs
← العودة إلى Kotlin Multiplatform Academy