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.

4.7 (6) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

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:

  1. Aliases Controller as UIViewController.
  2. Writes getBool(), which reads a Boolean from NSUserDefaults.
  3. Declares setBool() to set a Boolean on NSUserDefaults.

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:

  1. Updates tableView(_, didSelectRowAt:) to toggle the Boolean for the item when the cell is selected.
  2. In tableView(_, cellForRowAt:), sets the accessoryType 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.

iOS App with Favorites

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!