Xamarin Forms iOS Toast Message

In my previous article, I explained how toast messages for Android. In this article, I will refer to a topic that is not much available on the internet. In fact, many nuget packages available to create toast messages may not seem to go into the present lukewarm views or output. That’s why the toast message we wrote is more appealing.

First of all, we will do these operations in iOS with Dependency Service like we did for Android. First, we need to create an interface at the portable layer of our project. We will do two things in the classes we inherited from this interface. In these, the toast message will be displayed on the screen for a long time and the second time will be displayed on the screen for a short time.

After creating an interface, we come to the iOS part of our project. We need to open a class here and inherit from the IMessage interface we created on the portable layer. Once inherited, we need to implement functions on the interface. To explain what we do here; we first set fixed times to generate long and short time toast messages. These are 3,5 seconds and 2 seconds. You can adjust them as you like. Then we create a Timer to count the time, and then we create an object from the UIAlert class to indicate the message on the screen. We create a function called MessageBas to print the desired message on the screen. This function receives two different variables from the outside, message and time. First we check the duration of the function. If the time is up, we’re on the screen. After we have done this check, we display the message on the screen. Of course, we also need to specify that this class is a dependency service. After you’ve mentioned it above your class, we have our toast message for iOS ready.

Now all that’s left is to try it. I create a page and drop a button here. Pressing this button will allow the screen to display the toast message.

The page’s screen display is as follows.

If you have any questions, you can contact me by email or comment. Good works.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.