Open App Store or Play Store in Xamarin Forms


Isn't it fun,

to instantly listen to a blog on the go? PLAY !

 
 

App-Store-or-Play-Store

Xamarin is an open-source platform where we can build a native user interface for iOS, Android, and Windows Phone using a single codebase with C#. With the use of these native APIs, we can easily read and write files. As we can see Xamarin is a cross-platform that’s why Xamarin forms works on multiple platforms like iOS, Android, UWP and they have their own file system.

Now, we are going to create an application for the open play store and app store. In this blog, we can create an application that opens the play store by using a device like an Android. And open the app store by using a device like an iOS. For open play store or app store in Xamarin app, there is one prerequisite that uses Visual Studio 2017 or later version (Windows or Mac).

Now, we can create a project for an open play store in our Xamarin app. First, open Visual Studio 2017 or the latest version then select a mobile app. After that select the path when you want to store your app and give the appropriate name to the app. Then you will see platforms and templates for the app. So you can select blank app template and Android, iOS platform.

Note that, the application we are going to open in the Play Store or App store so this application is must be live in the Play Store or App Store both. In Xamarin forms, we need to add id, AppName with a location to open the app for both of the platforms Android and iOS.

For example, we open Twitter in our app so we need a Twitter id. For Android, the app id is = https://play.google.com/store/apps/details?id=com.twitter.android, And for iOS, the app id is = https://apps.apple.com/in/app/twitter/id333903271.

You can open a play store or app store using the below code.

For Android,

if (Device.RuntimePlatform == Device.Android)
{
url = "URL of Android Application";
       await Browser.OpenAsync(url, BrowserLaunchMode.External);
}

For iOS,

var Location = RegionInfo.CurrentRegion.Name.ToLower();
if (Device.RuntimePlatform == Device.iOS)
{
	url = https://itunes.apple.com/ " + Location + "https://itunes.apple.com/";

              await Browser.OpenAsync(url, BrowserLaunchMode.External);
}

Now, let’s see the full source code of the open App Store and Play Store in our Xamarin app.

MainPage.Xaml




    

    

        

            

In this Xaml, we can add an image and one button. On the button control, we can add clicked events. On the button click, we can open the Play Store or App Store.



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 AppStoreExample
{
    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";
                await Browser.OpenAsync(url, BrowserLaunchMode.External);
            }

            var Location = RegionInfo.CurrentRegion.Name.ToLower();
            if (Device.RuntimePlatform == Device.iOS)
            {
                url = "https://itunes.apple.com/ " + Location + "https://itunes.apple.com/";
                await Browser.OpenAsync(url, BrowserLaunchMode.External);
            }
        }

    }
}

Output:

Image: Open App Store or Play Store Example

So this is how we can open the App Store or Play Store on Android and iOS both. There is one alternative way to open App Store or Play Store on both the platform using Launcher.OpenAsync() this can be done by using Dependency Services.

For using dependency services, we need to create an interface for using the shared code. So first, we create an interface on a shared project. Just Right-click the shared project then click Add and then select Interface and give the name. The name of the interface starts with I (e.g. IAppStoreInterface). After creating the interface, we need to inherit this interface on the Android and iOS platforms. And add the assembly and use the method which we can create in the interface. Add below code for the user interface.

IStoreInterface.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace AppStoreExample
{
    public interface IStoreInterface
    {
        void OpenStore();
    }
}

Methods for Android and iOS platforms.


One Stop Solution for Xamarin Mobile App Development

Your Search ends here.


iOS Method

Launcher.OpenAsync
(New Uri("https://itunes.apple.com/in/app/udemy-130-000-online-courses/id562413829") ) ;

Android Method

Launcher.OpenAsync(
new Uri
("https://play.google.com/store/apps/details?id=com.udemy.android&hl=en"));

Below code is for the open Play Store on the Android platform.

PlayStoreImplement.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using AppStoreExample.Droid;
using Xamarin.Forms;

[assembly:Dependency(typeof(PlayStoreImplement))]
namespace AppStoreExample.Droid
{
    public class PlayStoreImplement : IStoreInterface
    {
        [Obsolete]
        public void OpenStore()
        {
            Device.OpenUri(new Uri("https://play.google.com/store/apps/details?id=com.sisystems.Sisystems"));
        }
    }
}

Below code is for the open App Store on the iOS platform.

AppStoreImplement.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppStoreExample.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
[assembly:Dependency(typeof(AppStoreImplement))]
namespace AppStoreExample.iOS
{
    public class AppStoreImplement : IStoreInterface
    {
        [Obsolete]
        public void OpenStore()
        {
            Device.OpenUri(new Uri("https://itunes.apple.com/in/app/facebook/id284882215?mt=8"));
        }
    }
}

We can see both of the ways to open the App Store or Play Store on Android and iOS platforms. In the first example, we can open the App Store or Play store on button click event. On the button click event first, we can check the platform (Android or iOS) then we can open the App Store or Play store using await Browser.OpenAsync(). This is one way to open the App Store or Play store and the second way to do this is by using Launcher.OpenAsync().

Conclusion

In this blog, we can see how to open the App Store or Play Store in our Xamarin app. We have seen two ways to open the App Store or Play Store, the first way is that when the user clicks the button it will call the button clicked event and on that event, we can call Browser.OpenAsync() method. And the second way to do this by Launcher.OpenAsync() method and this can be done using the dependency services. We have created an interface and implementing it on Android and iOS platforms.

App-Store-or-Play-Store

Xamarin is an open-source platform where we can build a native user interface for iOS, Android, and Windows Phone using a single codebase with C#. With the use of these native APIs, we can easily read and write files. As we can see Xamarin is a cross-platform that’s why Xamarin forms works on multiple platforms like iOS, Android, UWP and they have their own file system.

Now, we are going to create an application for the open play store and app store. In this blog, we can create an application that opens the play store by using a device like an Android. And open the app store by using a device like an iOS. For open play store or app store in Xamarin app, there is one prerequisite that uses Visual Studio 2017 or later version (Windows or Mac).

Now, we can create a project for an open play store in our Xamarin app. First, open Visual Studio 2017 or the latest version then select a mobile app. After that select the path when you want to store your app and give the appropriate name to the app. Then you will see platforms and templates for the app. So you can select blank app template and Android, iOS platform.

Note that, the application we are going to open in the Play Store or App store so this application is must be live in the Play Store or App Store both. In Xamarin forms, we need to add id, AppName with a location to open the app for both of the platforms Android and iOS.

For example, we open Twitter in our app so we need a Twitter id. For Android, the app id is = https://play.google.com/store/apps/details?id=com.twitter.android, And for iOS, the app id is = https://apps.apple.com/in/app/twitter/id333903271.

You can open a play store or app store using the below code.

For Android,

if (Device.RuntimePlatform == Device.Android)
{
url = "URL of Android Application";
       await Browser.OpenAsync(url, BrowserLaunchMode.External);
}

For iOS,

var Location = RegionInfo.CurrentRegion.Name.ToLower();
if (Device.RuntimePlatform == Device.iOS)
{
	url = https://itunes.apple.com/ " + Location + "https://itunes.apple.com/";

              await Browser.OpenAsync(url, BrowserLaunchMode.External);
}

Now, let’s see the full source code of the open App Store and Play Store in our Xamarin app.

MainPage.Xaml




    

    

        

            

In this Xaml, we can add an image and one button. On the button control, we can add clicked events. On the button click, we can open the Play Store or App Store.



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 AppStoreExample
{
    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";
                await Browser.OpenAsync(url, BrowserLaunchMode.External);
            }

            var Location = RegionInfo.CurrentRegion.Name.ToLower();
            if (Device.RuntimePlatform == Device.iOS)
            {
                url = "https://itunes.apple.com/ " + Location + "https://itunes.apple.com/";
                await Browser.OpenAsync(url, BrowserLaunchMode.External);
            }
        }

    }
}

Output:

Image: Open App Store or Play Store Example

So this is how we can open the App Store or Play Store on Android and iOS both. There is one alternative way to open App Store or Play Store on both the platform using Launcher.OpenAsync() this can be done by using Dependency Services.

For using dependency services, we need to create an interface for using the shared code. So first, we create an interface on a shared project. Just Right-click the shared project then click Add and then select Interface and give the name. The name of the interface starts with I (e.g. IAppStoreInterface). After creating the interface, we need to inherit this interface on the Android and iOS platforms. And add the assembly and use the method which we can create in the interface. Add below code for the user interface.

IStoreInterface.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace AppStoreExample
{
    public interface IStoreInterface
    {
        void OpenStore();
    }
}

Methods for Android and iOS platforms.


One Stop Solution for Xamarin Mobile App Development

Your Search ends here.


iOS Method

Launcher.OpenAsync
(New Uri("https://itunes.apple.com/in/app/udemy-130-000-online-courses/id562413829") ) ;

Android Method

Launcher.OpenAsync(
new Uri
("https://play.google.com/store/apps/details?id=com.udemy.android&hl=en"));

Below code is for the open Play Store on the Android platform.

PlayStoreImplement.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using AppStoreExample.Droid;
using Xamarin.Forms;

[assembly:Dependency(typeof(PlayStoreImplement))]
namespace AppStoreExample.Droid
{
    public class PlayStoreImplement : IStoreInterface
    {
        [Obsolete]
        public void OpenStore()
        {
            Device.OpenUri(new Uri("https://play.google.com/store/apps/details?id=com.sisystems.Sisystems"));
        }
    }
}

Below code is for the open App Store on the iOS platform.

AppStoreImplement.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppStoreExample.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
[assembly:Dependency(typeof(AppStoreImplement))]
namespace AppStoreExample.iOS
{
    public class AppStoreImplement : IStoreInterface
    {
        [Obsolete]
        public void OpenStore()
        {
            Device.OpenUri(new Uri("https://itunes.apple.com/in/app/facebook/id284882215?mt=8"));
        }
    }
}

We can see both of the ways to open the App Store or Play Store on Android and iOS platforms. In the first example, we can open the App Store or Play store on button click event. On the button click event first, we can check the platform (Android or iOS) then we can open the App Store or Play store using await Browser.OpenAsync(). This is one way to open the App Store or Play store and the second way to do this is by using Launcher.OpenAsync().

Conclusion

In this blog, we can see how to open the App Store or Play Store in our Xamarin app. We have seen two ways to open the App Store or Play Store, the first way is that when the user clicks the button it will call the button clicked event and on that event, we can call Browser.OpenAsync() method. And the second way to do this by Launcher.OpenAsync() method and this can be done using the dependency services. We have created an interface and implementing it on Android and iOS platforms.