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.
public interface IMessage { void UzunMesaj(string mesaj); void KisaMesaj(string mesaj); }
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.
[assembly:Xamarin.Forms.Dependency(typeof(iOSMesaj))] namespace iOS.ConnectionHelper { public class iOSMesaj : IMessage { const double LONG_DELAY = 3.5; const double SHORT_DELAY = 2.0; NSTimer alertDelay; UIAlertController alert; // Kısa mesaj basıyoruz public void KisaMesaj(string mesaj) { MesajBas(mesaj, SHORT_DELAY); } // Uzun mesaj yazdırıyoruz public void UzunMesaj(string mesaj) { MesajBas(mesaj, LONG_DELAY); } void MesajBas(string mesaj, double sure) { alertDelay = NSTimer.CreateRepeatingScheduledTimer(sure, (obj) => { MesajReddet(); }); alert = UIAlertController.Create(null, mesaj, UIAlertControllerStyle.Alert); UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null); } void MesajReddet() { if (alert!=null) { alert.DismissViewController(true, null); } if (alertDelay!=null) { alertDelay.Dispose(); } } }
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.
void buttonBas_Clicked(object sender,EventArgs e) { DependencyService.Get<IMessage>().UzunMesaj("Uzun mesajdır"); }
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 Reply