{"id":887,"date":"2018-02-13T22:19:51","date_gmt":"2018-02-13T22:19:51","guid":{"rendered":"http:\/\/sezeromer.com\/?p=887"},"modified":"2023-02-26T12:57:02","modified_gmt":"2023-02-26T09:57:02","slug":"xamarin-forms-ios-toast-mesaj","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/xamarin-forms-ios-toast-mesaj\/","title":{"rendered":"Xamarin Forms iOS Toast Message"},"content":{"rendered":"<p>In my previous article, I explained how <strong>toast<\/strong> messages for <strong>Android<\/strong>. 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&#8217;s why the toast message we wrote is more appealing.<\/p>\n<p>First of all, we will do these operations in <strong>iOS<\/strong> with <strong>Dependency<\/strong> <strong>Service<\/strong> like we did for <strong>Android<\/strong>. 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.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public interface IMessage\r\n    {\r\n        void UzunMesaj(string mesaj);\r\n        void KisaMesaj(string mesaj);\r\n\r\n    }<\/pre>\n<\/div>\n<p>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&#8217;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&#8217;ve mentioned it above your class, we have our toast message for iOS ready.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[assembly:Xamarin.Forms.Dependency(typeof(iOSMesaj))]\r\nnamespace iOS.ConnectionHelper\r\n{\r\n    public class iOSMesaj : IMessage\r\n    {\r\n        const double LONG_DELAY = 3.5;\r\n        const double SHORT_DELAY = 2.0;\r\n\r\n        NSTimer alertDelay;\r\n        UIAlertController alert;\r\n\r\n        \/\/ K\u0131sa mesaj bas\u0131yoruz\r\n        public void KisaMesaj(string mesaj)\r\n        {\r\n            MesajBas(mesaj, SHORT_DELAY);\r\n        }\r\n\r\n        \/\/ Uzun mesaj yazd\u0131r\u0131yoruz\r\n        public void UzunMesaj(string mesaj)\r\n        {\r\n            MesajBas(mesaj, LONG_DELAY);\r\n\r\n        }\r\n\r\n        void MesajBas(string mesaj, double sure)\r\n        {\r\n            alertDelay = NSTimer.CreateRepeatingScheduledTimer(sure, (obj) =&gt;\r\n              {\r\n                  MesajReddet();\r\n              });\r\n            alert = UIAlertController.Create(null, mesaj, UIAlertControllerStyle.Alert);\r\n\r\n            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);\r\n\r\n        }\r\n\r\n        void MesajReddet()\r\n        {\r\n            if (alert!=null)\r\n            {\r\n                alert.DismissViewController(true, null);\r\n\r\n            }\r\n            if (alertDelay!=null)\r\n            {\r\n                alertDelay.Dispose();\r\n            }\r\n        }\r\n\r\n        \r\n    }<\/pre>\n<\/div>\n<p>Now all that&#8217;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.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"> void buttonBas_Clicked(object sender,EventArgs e)\r\n        {\r\n            DependencyService.Get&lt;IMessage&gt;().UzunMesaj(\"Uzun mesajd\u0131r\");\r\n\r\n        }<\/pre>\n<\/div>\n<p>The page&#8217;s screen display is as follows.<\/p>\n<p><a href=\"http:\/\/sezeromer.com\/xamarin-forms-ios-toast-mesaj\/toast-1\/\" rel=\"attachment wp-att-892\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-892\" src=\"http:\/\/sezeromer.com\/wp-content\/uploads\/2018\/02\/toast-1.png\" alt=\"\" width=\"320\" height=\"715\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/02\/toast-1.png 320w, https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/02\/toast-1-134x300.png 134w\" sizes=\"(max-width: 320px) 100vw, 320px\" \/><\/a><\/p>\n<p>If you have any questions, you can contact me by email or comment. Good works.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&#8217;s why the toast message [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":892,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288,3],"tags":[114,293,115,7,270,22,119],"class_list":["post-887","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-xamarin-ile-yapilmis-projeler","tag-android","tag-dependency-service","tag-ios","tag-omer-sezer","tag-toast","tag-xamarin","tag-xamarin-forms"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/887"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=887"}],"version-history":[{"count":7,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/887\/revisions"}],"predecessor-version":[{"id":2570,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/887\/revisions\/2570"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/892"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}