KotlinからApple Frameworksを使う
platform.Foundationなどへ直接アクセスします
「KotlinからApple Frameworksを使う」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Apple APIs, in Kotlin
Kotlin/Native ships with ready-made bindings for Apple platforms. From iosMain you can call Foundation, UIKit and more directly in Kotlin. 🍎
The platform Packages
Every Apple framework lives under a platform package. Import platform.Foundation, platform.UIKit, or platform.CoreLocation just like any Kotlin package.
import platform.Foundation.NSDate
import platform.UIKit.UIDeviceA First Foundation Call
Try NSDate. Creating one gives you the current moment, exactly as Swift would, but the code stays in your shared iosMain source set.
val now = NSDate()
println(now.description())UIKit Without Leaving Kotlin
Need the device name or OS version? UIDevice.currentDevice hands it over so platform code reads naturally in Kotlin.
val v = UIDevice.currentDevice.systemVersion
println("iOS " + v)iosMain Is the Right Home
These bindings exist only on iOS targets. Put any Apple-framework call in iosMain, never in commonMain, or the Android build breaks.
Objective-C Becomes Kotlin
The Kotlin/Native compiler reads Apple headers and generates Kotlin signatures. An Objective-C class arrives as a normal Kotlin class you can construct.
Methods Map to Functions
Objective-C selectors turn into Kotlin functions. A long selector like stringWithFormat: becomes a tidy named function you call the usual way. ✨
Nullability Carries Over
Apple nullability annotations map to Kotlin types. A nullable Objective-C value becomes a Kotlin nullable type, so the compiler still guards you.
val url: NSURL? = NSURL.URLWithString("https://x.io")Constants and Enums
Framework constants and enums come across too. An NSError domain string or an enum case is reachable straight from Kotlin code.
Versions and Availability
Bindings track an iOS SDK version. Newer Apple APIs only appear if your deployment target and SDK are recent enough to expose them.
Keep the Boundary Thin
Call Apple APIs in a small iosMain wrapper, then expose a clean Kotlin result. Hiding platform detail behind one function keeps shared code tidy.
Quick Check
A quick one on where Apple framework calls belong.
Recap: Apple from Kotlin
You saw that Kotlin/Native exposes Apple frameworks under platform packages, callable from iosMain with familiar Kotlin syntax. Keep them behind a thin wrapper. 🎉
よくある質問
「KotlinからApple Frameworksを使う」レッスンは無料ですか?
はい。「KotlinからApple Frameworksを使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「KotlinからApple Frameworksを使う」で何を学びますか?
platform.Foundationなどへ直接アクセスします ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「KotlinからApple Frameworksを使う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- KotlinからApple Frameworksを使う
- Kotlin/NativeのMemoryとARC
- Cライブラリとのcinterop
- ネイティブSDKをきれいにラップする