Xamarin.Forms - Open App Store Or Play Store In XamarinApp

Introduction

 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 
Xamarin.Forms - Open App Store Or Play Store In XamarinApp
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You will learn more by going through the steps yourself.
 
Create a new or existing Xamarin forms(.Net standard) Project for Android and iOS Platform.
 
Xamarin.Forms - Open App Store Or Play Store In XamarinApp
 
Note
Your app must be live in the App Store and Playstore
 
Android
 
Here, you need the Android app id Ex:com.twitter.android.
 
I'm using Twitter for this example.
 
Xamarin.Forms - Open App Store Or Play Store In XamarinApp
https://play.google.com/store/apps/details?id=com.twitter.android
 
Code
  1. if (Device.RuntimePlatform == Device.Android)  
  2. {  
  3. url = "https://play.google.com/store/apps/details?id=com.sisystems.Sisystems";  
  4. await Browser.OpenAsync(url, BrowserLaunchMode.External);  
  5. }  
iOS
 
Here, you need AppName and AppId with a location. You will get appname and aped from apple developer connect.
 
Xamarin.Forms - Open App Store Or Play Store In XamarinApp
https://apps.apple.com/in/app/twitter/id333903271
 
Code
  1. var location = RegionInfo.CurrentRegion.Name.ToLower();  
  2. if (Device.RuntimePlatform == Device.iOS)  
  3. {  
  4.    url = "https://itunes.apple.com/" + location + "/app/twitter/id333903271?mt=8";  
  5.    await Browser.OpenAsync(url, BrowserLaunchMode.External);  
  6. }  
Simple UI
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:d="http://xamarin.com/schemas/2014/forms/design"    
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.              xmlns:TitleView="clr-namespace:XamarinApp.CustomView"    
  7.              mc:Ignorable="d" x:Class="XamarinApp.MainPage">    
  8.     
  9.     <NavigationPage.TitleView>    
  10.       <TitleView:TitleView/>    
  11.     </NavigationPage.TitleView>    
  12.     
  13.     <StackLayout Margin="0,100,0,0" VerticalOptions="StartAndExpand">    
  14.         <Image VerticalOptions="Center" Source="xamarinmonkeysbanner.png"/>    
  15.     
  16.         <Button Text="Update your app" Clicked="Button_Clicked" />    
  17.             
  18.     </StackLayout>    
  19. </ContentPage>     
Full Source Code
 
MainPage.xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Globalization;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Xamarin.Essentials;  
  9. using Xamarin.Forms;  
  10.   
  11. namespace XamarinApp  
  12. {  
  13.     // Learn more about making custom code visible in the Xamarin.Forms previewer  
  14.     // by visiting https://aka.ms/xamarinforms-previewer  
  15.     [DesignTimeVisible(false)]  
  16.     public partial class MainPage : ContentPage  
  17.     {  
  18.         public MainPage()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.   
  23.         async void Button_Clicked(System.Object sender, System.EventArgs e)  
  24.         {  
  25.               
  26.             string url = string.Empty;  
  27.             var location = RegionInfo.CurrentRegion.Name.ToLower();  
  28.             if (Device.RuntimePlatform == Device.Android)  
  29.                 url = "https://play.google.com/store/apps/details?id=com.sisystems.Sisystems";  
  30.             else if (Device.RuntimePlatform == Device.iOS)  
  31.                 url = "https://itunes.apple.com/" + location + "/app/contractor-action-solution/id1039202852?mt=8";  
  32.             await Browser.OpenAsync(url, BrowserLaunchMode.External);  
  33.         }  
  34.   
  35.          
  36.     }  
  37. }  
Alternate way
 
You can use Lancher.OpenAsync also for opening the App Store and Play store.
  1. Launcher.OpenAsync(new Uri("https://itunes.apple.com/in/app/facebook/id284882215?mt=8"));  
Using Dependency Service
 
Interface
 
Create an interface for open app store or play store using a dependency service.
  1. using System;  
  2. jnamespace XamarinApp  
  3. {  
  4.     public interface IOpenAppStore  
  5.     {  
  6.           
  7.         void OpenAppStore();  
  8.     }  
  9. }  
iOS Implementation
 
Below sample code for the open App store in App iOS.
 
AppStoreImplementation.cs 
  1. [assembly:Dependency(typeof(AppStoreImplementation))]  
  2. namespace XamarinApp.iOS  
  3. {  
  4.     public class AppStoreImplementation : IOpenAppStore  
  5.     {  
  6.         public void OpenAppStore()  
  7.         {  
  8.             Device.OpenUri(new Uri("https://itunes.apple.com/in/app/facebook/id284882215?mt=8"));  
  9.         }  
  10.   
  11.     }  
  12. }  
Android Implementation
 
Below is the sample code for opening the play store in an Android app.
  1. [assembly:Dependency(typeof(AppStoreImplementation))]  
  2. namespace XamarinApp.Android  
  3. {  
  4.     public class AppStoreImplementation : IOpenAppStore  
  5.     {  
  6.         public void OpenAppStore()  
  7.         {  
  8.             Device.OpenUri(new Uri("https://play.google.com/store/apps/details?id=com.sisystems.Sisystems"));  
  9.         }  
  10.   
  11.     }  
  12. }  
Debug your App
 
Xamarin.Forms - Open App Store Or Play Store In XamarinApp
 
I hope you have understood how to open the Play Store or App Store in XamarinApp.
 
Thanks for reading. Please share your comments and feedback. Happy Coding :)


Similar Articles