In this article, you will learn how to use the shared framework in the IOS app. If you have not seen how to create the shared library then read my previous article.

To be able to use the shared framework in an IOS app, we need to make a few changes to our QuoteResource.kt file. We need to make the dispatcher platform specific because the Main Dispatcher works only for Android so let’s mark it as expect and provide different implementation to it.

Let’s start by changing our src/androidMain/kotlin/actual.kt for Android to:

Similarly, change the src/iosMain/Kotlin/actual.kt to

If you are facing any problem then check out the entire code in the git repository.

The shared library is ready to be used in our IOS app. Run the following script in the terminal to create the framework.

./gradlew :SharedCode:packForXcode

In our IOS app, we can start by adding the three labels in our Main.storyBoard using the same technique as we learned in our previous article. Connect those labels with the ViewController by right-clicking and dragging the point from New Reference outlet to the ViewController as shown in the figure below.

Furthermore, call our method getData() from ViewController which will read the data from an API and set the quoteMessage label using the following line of code.  

Finally, run the app and you will see that the data is correctly displayed in the app.

I hope these articles were useful and you enjoyed learning multi-platform application development with me. Happy reading!

Tags

One response

Leave a Reply

Your email address will not be published. Required fields are marked *