【Compose Multiplatform】Using CocoaPods in CMP Projects and Using iOS Swift/Obj-C Without CocoaPods
When developing cross-platform apps for both platforms the current official solutions may not be completely comprehensive so for some features we need to bridge back to the original platform and use that platform’s native code How can we implement this?
目錄
- 【Compose Multiplatform】專案轉移探討與開發指南
- 【Compose Multiplatform】手機使用依賴注入Koin
- 【Compose Multiplatform 】跨平台App但Android需要context作法並搭配Koin
- 【Compose Multiplatform】手機本地持久化儲存DataStore實作
- 【Compose Multiplatform】手機資料庫SqlDelight實作
- 【Compose Multiplatform】CMP中使用ROOM開發資料庫 - [KSP2] Annotation value is missing in nested annotations
- 【Compose Multiplatform】CMP專案中導入CocoaPods及無CocoaPods情況下使用IOS Swift/Obj-C
Add configuration to libs.version.toml
In build.gradle.kts
add the cocoapods block
and add the Pod library dependencies you want to use pod("Google-Mobile-Ads-SDK")
as well as the PodFile
location podfile = project.file("../iosApp/Podfile")
usually under your iOS project
if you generate a project with the KMM official website
the default name is iosApp
The official website mentions that you can add the following types of Pod library dependencies
- From CocoaPods repository
- Locally stored libraries
- Custom Git repository
- Custom Podspec repository
- With custom cinterop options
Configure Podfile under the iosApp project
After that
use pod install
in the iosApp project to install the configuration
if it’s already installed, you can use pod install --repo-update
or pod deintegrate
and then reinstall
Finally, run ./gradlew build
if successful, you can import iOS bridged code through Kotlin
If you don’t want to use CocoaPods
you can use cinterop to create Kotlin bridges for Objective-C or Swift declarations
so you can call them from Kotlin
Step 1.
Create a .def
file in ../composeApp/nativeInterop/cinterop/xxx.def
(write the content according to your actual needs)
Step 2.
Add the following code to the iOS configuration in Build.gradle.kts
the framework you want to use can also be found through paths after installing with cocoapods
or you can implement your own .swift bridge
- However, based on current testing
iOS native code transferred directly through cocoapods configuration
cannot be completely transferred at the current stage
for example, when I useGoogle-Mobile-Ads-SDK
, not all functions can be resolved - So we need to use .def to transfer it ourselves
but this is equivalent to still having to write iOS native code