Kotlin Multiplatform
for iOS Developers
Dinorah Tovar
Platform Mobile Engineer
@ konfío.mx
@ddinorahtovar
@ddinorahtovar
Slide 2
Slide 2 text
Kotlin and Swift are
like cousins
Slide 3
Slide 3 text
Like cousins?
@ddinorahtovar
public var description: String {
switch self {
case .active:
return "active"
case .offer:
return "offer"
case .uploadDocuments:
return "documents"
case .waitingDisbursement:
return "waitingDisbursement"
case .former:
return "former"
}
}
fun description(it: String) =
when (this) {
active #-> "active"
offer #-> "offer"
uploadDocuments #-> “documents"
waitingDisbursement #->
“waitingDisbursement"
former #-> “former”
}
Slide 4
Slide 4 text
Kotlin is not for Android
only
Slide 5
Slide 5 text
Kotlin is for multiple
things!
Slide 6
Slide 6 text
Kotlin is for Multiple things
@ddinorahtovar
Mobile Cross
platform
Server Side Android
Native Web Development Data Science
Slide 7
Slide 7 text
KMP is for Business Logic
@ddinorahtovar
KMP
Business Logic Models
Slide 8
Slide 8 text
Kotlin is for Multiple things
@ddinorahtovar
JAR/AAR (For Android)
Kotlin
Multiplatform
Kotlin Compiler
Native
JS
JVM
Binaries for iOS
Or Windows
Slide 9
Slide 9 text
Kotlin is for Multiple things
@ddinorahtovar
Native
Binaries for iOS
Relies on Objective C
Objective-C headers that are annotated for Swift
Slide 10
Slide 10 text
What to expect from KMP
@ddinorahtovar
Name Folder Target Result
Common
iOS
Android
SharedCode/commonMain/kotlin
SharedCode/iosMain
SharedCode/androidMain/kotlin
-
iOS arm 64
or
x86_64
JVM 1.6
Kotlin metadata
Apple framework
.jar file or
.class files
Slide 11
Slide 11 text
What to expect from KMP
@ddinorahtovar
Kotlin Multiplatform
First Gen Support
Android and iOS
SDK
Slide 12
Slide 12 text
Sharing the logic!
@ddinorahtovar
expect fun / class
actual fun / class declaration
Import iOS libraries
actual fun / class declaration
Import JVM libraries
Slide 13
Slide 13 text
Sharing the logic!
@ddinorahtovar
expect fun saveValue(value: String)
actual fun saveValueLocally(value: String) {
NSUserDefaults.standardUserDefaults.setValue(
value,
forKey = "Value"
)
}
actual fun saveValueLocally(value: String) {
sharedPreferences.edit {
putString("Value", value)
}
}
Slide 14
Slide 14 text
Sharing the logic!
@ddinorahtovar
Ktor / Kotlin
Serialization
For IO
Apollo
For GraphQL
SQLDelight
Kodein
SQLiter
For Databases
Kotlinx-dateTime
For Dates
Slide 15
Slide 15 text
Kotlin has Coroutines
Slide 16
Slide 16 text
Coroutines
@ddinorahtovar
•Suspended computation
•Coroutines support!
•Cold & Hot Streams
Coroutines
@ddinorahtovar
suspend fun getProducts() {}
fun getProducts(callback: List #-> Unit) {
GlobalScope.launch(ApplicationDispatcher) {
callback(model.getProducts())
}
}
getProducts{ (products: [Product]) #-> () in
#// Do something here with product
}
iOS Call from Controller
Suspend function in Kotlin Common
suspend fun getProducts() {}
Coroutine + Dispatcher
Slide 19
Slide 19 text
Databases
Slide 20
Slide 20 text
Sharing Databases
@ddinorahtovar
CREATE TABLE ProductList(
product_id INTEGER NOT NULL PRIMARY KEY,
product_name TEXT NOT NULL
);
insertItem:
INSERT OR REPLACE INTO ProductList(
product_id,
product_name
)VALUES(?,?);
Slide 21
Slide 21 text
Sharing Databases
@ddinorahtovar
expect fun createDb() : ProductDB
class ProductsRepository {
private val productApi = ProductApi()
private val productDB = createDb()
private val productQueries = productDB.queries
suspend fun fetchProducts() {
val productsAvailable = productApi.fetchProducts()
productsAvailable.forEach {
productQueries.insertItem(
it.product_id.toLong(),
it.product_name
)
}
}
}
Slide 22
Slide 22 text
Sharing Databases
@ddinorahtovar
expect fun createDb() : ProductDB
actual fun createDb(): MyDatabase {
val products = NativeSqliteDriver(MyDatabase.Schema, “products.db”)
return MyDatabase(driver)
}
Slide 23
Slide 23 text
Sharing Databases
@ddinorahtovar
let productRepository = ProductRepository()
productRepository.fetchProduct(success: { data in
self.listProduct = data
self.tableView.reloadData()
return KotlinUnit()
})
actual fun createDb(): MyDatabase {
val products = NativeSqliteDriver(MyDatabase.Schema, “products.db”)
return MyDatabase(driver)
}
Slide 24
Slide 24 text
Give it a try!
Slide 25
Slide 25 text
Kotlin Multiplatform
for iOS Developers
Dinorah Tovar
Platform Mobile Engineer
@ konfío.mx
@ddinorahtovar
@ddinorahtovar