Introduction
Xamarin.Forms code runs on multiple platforms - each of which has its 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.
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 platforms.
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.
https://play.google.com/store/apps/details?id=com.twitter.android
Code
if (Device.RuntimePlatform == Device.Android)
{
string url = "https://play.google.com/store/apps/details?id=com.sisystems.Sisystems";
await Browser.OpenAsync(url, BrowserLaunchMode.External);
}
IOS
Here, you need AppName and AppId with a location. You will get the 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)
{
string 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.sisystems.Sisystems";
else if (Device.RuntimePlatform == Device.iOS)
url = "https://itunes.apple.com/" + location + "/app/contractor-action-solution/id1039202852?mt=8";
await Browser.OpenAsync(url, BrowserLaunchMode.External);
}
}
}
Alternate way
You can use Launcher.OpenAsync is also for opening the App Store and Play Store.
Launcher.OpenAsync(new Uri("https://itunes.apple.com/in/app/facebook/id284882215?mt=8"));
Using dependency service
Interface
Create an interface for the open app store or Play store using a dependency service.
using System;
jnamespace XamarinApp
{
public interface IOpenAppStore
{
void OpenAppStore();
}
}
IOS Implementation
Below sample code for the open App Store in App iOS.
AppStoreImplementation.cs
[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 is the sample code for opening the Play Store in an Android app.
[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.sisystems.Sisystems"));
}
}
}
Debug your App
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 :)