Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Multiplatform for iOS Developers (Lightn...

Kotlin Multiplatform for iOS Developers (Lightning talk)

In this talk, we will check some tricks inside the incredible world of Kotlin Multiplatform for iOS. Kotlin Multiplatform Mobile (KMM) is an SDK that allows you to use the same business logic code in both iOS and Android applications.

Dinorah Tovar

October 30, 2020
Tweet

More Decks by Dinorah Tovar

Other Decks in Technology

Transcript

  1. 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” }
  2. Kotlin is for Multiple things @ddinorahtovar Mobile Cross platform Server

    Side Android Native Web Development Data Science
  3. Kotlin is for Multiple things @ddinorahtovar JAR/AAR (For Android) Kotlin

    Multiplatform Kotlin Compiler Native JS JVM Binaries for iOS Or Windows
  4. Kotlin is for Multiple things @ddinorahtovar Native Binaries for iOS

    Relies on Objective C Objective-C headers that are annotated for Swift
  5. 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
  6. Sharing the logic! @ddinorahtovar expect fun / class actual fun

    / class declaration Import iOS libraries actual fun / class declaration Import JVM libraries
  7. 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) } }
  8. Sharing the logic! @ddinorahtovar Ktor / Kotlin Serialization For IO

    Apollo For GraphQL SQLDelight Kodein SQLiter For Databases Kotlinx-dateTime For Dates
  9. Coroutines @ddinorahtovar suspend fun getProducts() {} fun getProducts(callback: List<Product> #->

    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
  10. 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(?,?);
  11. 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 ) } } }
  12. Sharing Databases @ddinorahtovar expect fun createDb() : ProductDB actual fun

    createDb(): MyDatabase { val products = NativeSqliteDriver(MyDatabase.Schema, “products.db”) return MyDatabase(driver) }
  13. 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) }