Kotlin Multiplatform Project for Android and iOS: Getting Started
In this tutorial, you’ll learn how to use Kotlin Multiplatform and build an app for Android and iOS with the same business logic code. By JB Lorenzo.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Kotlin Multiplatform Project for Android and iOS: Getting Started
30 mins
- Getting Started
- Multiplatform
- Common Code
- Platform-Specific Code
- Integrating KMM Into an Existing Project
- Setting Up the Common Module
- Integrating Into Android
- Integrating Into iOS
- Fetching Data From the Network in Common Code
- Using expect in Common Modules
- Fetching Data in Android
- Fetching Data in iOS
- Saving Data in SharedPreferences and UserDefaults
- Saving Data in Android
- Saving Data in iOS
- Where to Go From Here?
Saving Data in iOS
Of course, this isn’t complete without the iOS part. Create a file called KeyValueStore.kt under shared/src/iosMain/kotlin/com.raywenderlich.android.multigrain.shared. Then insert these lines:
// 1
actual typealias Controller = UIViewController
// 2
actual fun Controller.getBool(key: String): Boolean {
return NSUserDefaults.standardUserDefaults.boolForKey(key)
}
// 3
actual fun Controller.setBool(key: String, value: Boolean) {
NSUserDefaults.standardUserDefaults.setBool(value, key)
}
Similar to the code in Android, the code above:
- Aliases
Controller
asUIViewController.
- Writes
getBool()
, which reads a Boolean fromNSUserDefaults.
- Declares
setBool()
to set a Boolean onNSUserDefaults
.
To show this, open MultiGrainsViewController+UITableView.swift in iosApp. Then modify it like below:
extension MultiGrainsViewController: UITableViewDelegate {
// 1
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
let entry = grainList[indexPath.row]
let current = api.isFavorite(id: entry.id)
api.setFavorite(id: entry.id, value: !current)
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
extension MultiGrainsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
// 2
cell.accessoryType = api.isFavorite(id: entry.id) ?
.checkmark : .none
return cell
}
}
Here’s a quick overview of what this code does:
- Updates
tableView(_, didSelectRowAt:)
to toggle the Boolean for the item when the cell is selected. - In
tableView(_, cellForRowAt:)
, sets theaccessoryType
to show a check if the item is a favorite.
Another small update is in MultiGrainsViewController.swift:
override func viewDidLoad() {
super.viewDidLoad()
api = GrainApi(context: self)
...
}
Now the API takes in a UIViewController
.
Finally, build and run your iosApp
in Android Studio. As with Android, you can now toggle your preferences and check if the data is persisted after a restart of the app.
If you see a something similar to the image above, it’s time to celebrate! You’ve done a good job. Grab a wheat beer if you like, or a granola bar. You’ve consumed a lot of calories while reading, and of course you need some reward. Enjoy!
Where to Go From Here?
Download the completed project by clicking the Download Materials button at the top or bottom of the tutorial.
To learn more about KMM, check out the Kotlin documentation. It also includes the release notes for the KMM plugin. Additionally, you’ll find notes for what’s new in Kotlin for KMM.
If you’re still hungry for more information, enjoy this detailed read on integrating KMM in an existing app.
We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!