{"id":47823,"date":"2020-08-19T11:30:00","date_gmt":"2020-08-19T18:30:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/xamarin\/?p=47823"},"modified":"2020-09-28T09:54:47","modified_gmt":"2020-09-28T16:54:47","slug":"first-run-xamarin-essentials","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/first-run-xamarin-essentials\/","title":{"rendered":"App First Run Detection with Xamarin.Essentials"},"content":{"rendered":"<p>One common scenario that developers often need to integrate into an app, is showing a prompt when an app is launched for the first time. This could be to display a disclaimer or to walk users through the functionality of the app. This can be done on first launch of the app. Even after install or for a specific version. Using <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials?WT.mc_id=firstrun-blog-jamont\">Xamarin.Essentials<\/a> helps integrate this functionality with a just few lines of code.<\/p>\n<h2>First Run Preferences<\/h2>\n<p>A way to determine the first run is to leverage the built-in <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials\/preferences?WT.mc_id=firstrun-blog-jamont\">preferences API<\/a>. This stores a boolean value that can be checked at runtime. For example, create a <code>Settings<\/code> class with a <code>FirstRun<\/code> property set the default value to <code>true<\/code>:<\/p>\n<pre><code class=\"csharp\">public static class Settings\n{\n  public static bool FirstRun\n  {\n      get =&gt; Preferences.Get(nameof(FirstRun), true);\n      set =&gt; Preferences.Set(nameof(FirstRun), value);\n  }\n}\n<\/code><\/pre>\n<p>Next, check the value at startup to confirm if the value is <code>true<\/code>. Then perform an action and set the value to <code>false<\/code>:<\/p>\n<pre><code class=\"csharp\">if(Settings.FirstRun)\n{\n   \/\/ Perform an action such as a \"Pop-Up\".\n   Settings.FirstRun = false;\n}\n<\/code><\/pre>\n<p>This is a great approach. That preference will persist for as long as the app continues to install. The downside is the &#8220;one-and-done&#8221; approach and would be difficult to check for specific versions. There are many things that you can do with preferences! Be sure to checkout the full <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials\/preferences?WT.mc_id=firstrun-blog-jamont\">documentation<\/a> and short video on it:<\/p>\n<p><center>\n<\/center><\/p>\n<p><iframe src=\"https:\/\/channel9.msdn.com\/Shows\/XamarinShow\/Preferences-Essential-API-of-the-Week\/player\" width=\"560\" height=\"315\" allowfullscreen frameBorder=\"0\" title=\"Preferences (Xamarin.Essentials API of the Week) - Microsoft Channel 9 Video\"><\/iframe><\/p>\n<h2>Version Tracking<\/h2>\n<p>The built-in <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials\/version-tracking?WT.mc_id=firstrun-blog-jamont\">version tracking API<\/a> offers rich functionality to detect first run. As well as for specific versions that the user is running. With this API, it is important to initialize <code>VersionTracking<\/code> early-on in your application. Version Tracking such as the <code>AppDelegate<\/code>, <code>MainActivity<\/code>, or the <code>App<\/code> constructor.<\/p>\n<pre><code class=\"csharp\">VersionTracking.Track();\n<\/code><\/pre>\n<p>After this is initialized, we can perform multiple checks to display different information:<\/p>\n<pre><code class=\"csharp\">if(VersionTracking.IsFirstLaunchEver)\n{\n  \/\/ Display pop-up alert for first launch\n}\nelse if(VersionTracking.IsFirstLaunchForCurrentVersion)\n{\n  \/\/ Display update notification for current version (1.0.0)\n}\nelse if(VersionTracking.IsFirstLaunchForCurrentBuild)\n{\n  \/\/ Display update notification for current build number (2)\n}\n<\/code><\/pre>\n<p>As you can see, there is a lot of powerful functionality built into this class. And this is just the beginning. It can also be used to check the current build, version number, history, and more! Check out the full <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials\/version-tracking?WT.mc_id=firstrun-blog-jamont\">documentation<\/a> to learn more about Xamarin.Essentials or watch the quick overview video:<\/p>\n<p><center>\n<\/center><\/p>\n<p><iframe src=\"https:\/\/channel9.msdn.com\/Shows\/XamarinShow\/Version-Tracking-XamarinEssentials-API-of-the-Week\/player\" width=\"560\" height=\"315\" allowFullScreen frameBorder=\"0\" title=\"Version Tracking (Xamarin.Essentials API of the Week) - Microsoft Channel 9 Video\"><\/iframe><\/p>\n<p>There you have it. Several great approaches to check if your app is being booted-up. For the very first run, or for a specific version. All using Xamarin.Essentials!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Several great approaches to check if and when your app is launching. For the very first run or for a specific version, all using Xamarin.Essentials! Easily detect the first time your user has run your application or for a specific version to easily integrate prompts.<\/p>\n","protected":false},"author":544,"featured_media":44710,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,556,1],"tags":[9010,9019,9154,27,9012,24],"class_list":["post-47823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-integrations","category-xamarin","tag-mobile-applications","tag-mobile-apps","tag-version-tracking","tag-xamarin","tag-xamarin-developers","tag-xamarin-essentials"],"acf":[],"blog_post_summary":"<p>Several great approaches to check if and when your app is launching. For the very first run or for a specific version, all using Xamarin.Essentials! Easily detect the first time your user has run your application or for a specific version to easily integrate prompts.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/47823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=47823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/47823\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/44710"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=47823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=47823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=47823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}