Xamarin.Forms - Open App Store or Play Store in XamarinApp | Xamarin Tutorial
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 are 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.
Prerequisites
- Visual Studio 2017 or later (Windows or Mac)
Setting up a Xamarin.Forms Project
Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform.
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 sample.
https://play.google.com/store/apps/details?id=com.twitter.android
Code
if (Device.RuntimePlatform == Device.Android)
{
url = "https://play.google.com/store/apps/details?id=com.twitter.android";
await Browser.OpenAsync(url, BrowserLaunchMode.External);
}
iOS
Here, you need AppName and AppId with a location you will get app name and aped from apple developer connect.
https://apps.apple.com/in/app/twitter/id333903271
Code
var location = RegionInfo.CurrentRegion.Name.ToLower();
if (Device.RuntimePlatform == Device.iOS)
{
url = "https://itunes.apple.com/" + location + "/app/twitter/id333903271?mt=8";
await Browser.OpenAsync(url, BrowserLaunchMode.External);
}
Simple UI
MainPage.xaml
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:TitleView="clr-namespace:XamarinApp.CustomView"
mc:Ignorable="d" x:Class="XamarinApp.MainPage">
<NavigationPage.TitleView>
<TitleView:TitleView/>
</NavigationPage.TitleView>
<StackLayout Margin="0,100,0,0" VerticalOptions="StartAndExpand">
<Image VerticalOptions="Center" Source="xamarinmonkeysbanner.png"/>
<Button Text="Update your app" Clicked="Button_Clicked" />
</StackLayout>
</ContentPage>
Full Source Code
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace XamarinApp
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
string url = string.Empty;
var location = RegionInfo.CurrentRegion.Name.ToLower();
if (Device.RuntimePlatform == Device.Android)
url = "https://play.google.com/store/apps/details?id=com.twitter.android";
else if (Device.RuntimePlatform == Device.iOS)
url = "https://itunes.apple.com/" + location + "/app/twitter/id333903271?mt=8";
await Browser.OpenAsync(url, BrowserLaunchMode.External);
}
}
}
Alternate way
You can use Lancher.OpenAsync also for opening App Store and Play store.
Launcher.OpenAsync(new Uri("https://itunes.apple.com/in/app/facebook/id284882215?mt=8"));
Using Dependency Service
Interface
using System;
namespace XamarinApp
{
public interface IOpenAppStore
{
void OpenAppStore();
}
}
iOS Implementation
Below sample code for open App store in App iOS.
[assembly:Dependency(typeof(AppStoreImplementation))]
namespace XamarinApp.iOS
{
public class AppStoreImplementation : IOpenAppStore
{
public void OpenAppStore()
{
Device.OpenUri(new Uri("https://itunes.apple.com/in/app/facebook/id284882215?mt=8"));
}
}
}
Android Implementation
Below sample code for open play store in App android.
[assembly:Dependency(typeof(AppStoreImplementation))]
namespace XamarinApp.Android
{
public class AppStoreImplementation : IOpenAppStore
{
public void OpenAppStore()
{
Device.OpenUri(new Uri("https://play.google.com/store/apps/details?id=com.twitter.android"));
}
}
}
Debug your app
I hope you have understood you will learn how to Open the Play Store or App Store in XamarinApp.
Thanks for reading. Please share your comments and feedback. Happy Coding :)