Open Play Store or App Store in Xamarin App using Xamarin.Forms


Isn't it fun,

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

 
 

Open Play Store or App Store

Xamarin.Forms code runs on multiple platforms like Android, iOS, UWP, and each of them has its own file system. Using native APIs, we can read or write files easily. Embedded resources are a simple solution for distributing data files with the application. In this blog, we will be going to create an application, in which we will open the play store by using the device as an android and App store by using the device as iOS. The prerequisite is Visual Studio 2017 or later versions (Mac or Windows).

First of all, we have to create a new Xamarin.Forms project or existing Xamarin.Forms project with iOS and Android platforms. Choose mobile app project from mobile. Give appropriate name to the application and then select blank app and platform - Android, iOS. Note that the app going to be open from Xamarin. Forms application is must be live in the Play Store and App store both. To open the app from our Xamarin.Forms application we need app id for Android and, AppName and AppId with a location for iOS. We can get AppId and AppName from Apple developer Connect.

Suppose we want to open the Twitter app, id for Android will be like "https://play.google.com/store/apps/details?id=com.twitter.android" and for iOS, it will be like " https://apps.apple.com/in/app/twitter/id333903271”.

Android Code:

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

iOS Code:

var Location = RegionInfo.CurrentRegion.Name.ToLower() ;
 if (Device.RuntimePlatform = = Device.iOS)
            {

                Url = "https://itunes.apple.com/ " + Location + "URL of iOS Application" ;
                await Browser.OpenAsync(Url, BrowserLaunchMode.External) ;
            }

Now we will be going to create a simple application, from which we will open the Play Store and App Store for the Udemy app. For that, we will create a new Xamarin.Forms project and apply the above code for both Android and iOS platforms. Create XAML page and apply below code and in code behind file apply MainPage.XAML.cs file code. URL for Udemy app:

https://play.google.com/store/apps/details?id=com.udemy.android&hl=en(Android)

https://play.google.com/store/apps/details?id=com.udemy.android&hl=en(iOS)

XAML file

<xmp>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPagexmlns= "http://xamarin.com/schemas/2014/forms"
xmlns:x= "http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local= "clr-namespace:OpenPlaystore"
xmlns:TitleView= "clr-namespace:XamarinApp.CustomView"
x:Class= "OpenPlaystore.MainPage"
>
<NavigationPage.TitleView>
<TitleView:TitleView/ >
</NavigationPage.TitleView>
<StackLayout>
<Button Text= "open Application from here" Clicked="Btn_Clicked"/>
</StackLayout>

</ContentPage>

</xmp>

Code-behind file

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace OpenPlaystore
{
    public partial class MainPage :ContentPage
    {


        public MainPage()
        {
            InitializeComponent();
        }

        async private void Btn_Clicked (object sender, 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.udemy.android&hl=en" ;
                await Browser.OpenAsync(Url, BrowserLaunchMode.External) ;
            }

            else if (Device.RuntimePlatform = = Device.iOS)
            {
                url = "https://itunes.apple.com/" + Location + "/app/udemy-130-000-online-courses/id562413829" ;
                await Browser.OpenAsync(Url, BrowserLaunchMode.External) ;
            }

        }
    }
}

This way we can open the play store or app store from our Xamarin.Forms application. An alternate way is by using Launcher.OpenAsync() for opening play Store and App Store both. We can do this by using Dependency Services.

Dependency Service is a Static class in the Xamarin Forms namespace. It is a service locator, we can invoke native platform functionality from shared code using service locator. To use dependency services, we have to create an interface in our shared project and use that interface in all platform-specific platforms. Inherit the created interface in platform-specific projects and add a reference to that class. These codes are for creating an interface and implementing an interface to the specific platforms. The first code is for creating the interface. We will create the interface in our shared project and inherit that interface in both platforms after that we have used the method called OpenAppPlayStore which is created in the interface. The second code is for implementing the interface in the iOS platform and the third for the Android platform.

Code for creating an interface:

using System;
namespace OpenPlaystore
{
    public interface IOpenAppPlayStore
    {

        void OpenAppPlayStore() ;
    }
}

Code for iOS platform:

[assembly:Dependency(typeof(AppStoreImplementationios))]
namespace XamarinApp.iOS
{
    public class AppStoreImplementationios :IOpenAppPlayStore
    {
        public void OpenAppPlayStore()
        {
            Device.OpenUri(new Uri("https://itunes.apple.com/in/app/udemy-130-000-online-courses/id562413829 ") ) ;
        }

    }
}

Code for Android Platform:

[assembly:Dependency(typeof(AppStoreImplementationan) ) ]
namespace XamarinApp.Android
{
    public class AppStoreImplementationan :IOpenAppPlayStore
    {
        public void OpenAppPlayStore()
        {
            Device.OpenUri(new Uri("https://play.google.com/store/apps/details?id=com.udemy.android&hl=en"));
        }

    }
} 

iOS Method

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

Android Method

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

Using these ways, we can open the Play Store and App store from Xamarin.Forms Application. Here, we have created an application to open the play store and App Store from the application. For that, we have used the await Browser.OpenAsync() on the button clicked method. When the user clicks the button, it will call the Btn_Clicked method and then check for device and then call await Browser.OpenAsync (). This is one way to open the Play Store and App store another way is to use Launcher.OpenAsync (). We have achieved this by using dependency services. We have seen how to use dependency services, first of all, we have created an interface named IOpenAppPlayStore in a shared project and create OpenAppPlayStore() method and implement that interface in both android and iOS platform. In the Android platform, we have created class AppStoreImplementationan and AppStoreImplementationios for the iOS platform. In both platforms we have inherited interface, add assembly, and use a method from interface created in a shared project.

Conclusion

In this blog, we have understood how to open the Play Store and App Store from our Xamarin.Forms Application. We have seen two ways to open the Play Store and App Store. We have created one application in which we built one button and when the user clicks the button it will call the button clicked method from code behind file and call Browse.OpenAsync(). This is one-way alternate way to open the play store and App store is to use Launcher.OpenAsync() method. To use this method, we have used dependency services, and also seen how to use dependency services. We have created an interface and implemented that interface on both platforms.

Open Play Store or App Store

Xamarin.Forms code runs on multiple platforms like Android, iOS, UWP, and each of them has its own file system. Using native APIs, we can read or write files easily. Embedded resources are a simple solution for distributing data files with the application. In this blog, we will be going to create an application, in which we will open the play store by using the device as an android and App store by using the device as iOS. The prerequisite is Visual Studio 2017 or later versions (Mac or Windows).

First of all, we have to create a new Xamarin.Forms project or existing Xamarin.Forms project with iOS and Android platforms. Choose mobile app project from mobile. Give appropriate name to the application and then select blank app and platform - Android, iOS. Note that the app going to be open from Xamarin. Forms application is must be live in the Play Store and App store both. To open the app from our Xamarin.Forms application we need app id for Android and, AppName and AppId with a location for iOS. We can get AppId and AppName from Apple developer Connect.

Suppose we want to open the Twitter app, id for Android will be like "https://play.google.com/store/apps/details?id=com.twitter.android" and for iOS, it will be like " https://apps.apple.com/in/app/twitter/id333903271”.

Android Code:

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

iOS Code:

var Location = RegionInfo.CurrentRegion.Name.ToLower() ;
 if (Device.RuntimePlatform = = Device.iOS)
            {

                Url = "https://itunes.apple.com/ " + Location + "URL of iOS Application" ;
                await Browser.OpenAsync(Url, BrowserLaunchMode.External) ;
            }

Now we will be going to create a simple application, from which we will open the Play Store and App Store for the Udemy app. For that, we will create a new Xamarin.Forms project and apply the above code for both Android and iOS platforms. Create XAML page and apply below code and in code behind file apply MainPage.XAML.cs file code. URL for Udemy app:

https://play.google.com/store/apps/details?id=com.udemy.android&hl=en(Android)

https://play.google.com/store/apps/details?id=com.udemy.android&hl=en(iOS)

XAML file

<xmp>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPagexmlns= "http://xamarin.com/schemas/2014/forms"
xmlns:x= "http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local= "clr-namespace:OpenPlaystore"
xmlns:TitleView= "clr-namespace:XamarinApp.CustomView"
x:Class= "OpenPlaystore.MainPage"
>
<NavigationPage.TitleView>
<TitleView:TitleView/ >
</NavigationPage.TitleView>
<StackLayout>
<Button Text= "open Application from here" Clicked="Btn_Clicked"/>
</StackLayout>

</ContentPage>

</xmp>

Code-behind file

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace OpenPlaystore
{
    public partial class MainPage :ContentPage
    {


        public MainPage()
        {
            InitializeComponent();
        }

        async private void Btn_Clicked (object sender, 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.udemy.android&hl=en" ;
                await Browser.OpenAsync(Url, BrowserLaunchMode.External) ;
            }

            else if (Device.RuntimePlatform = = Device.iOS)
            {
                url = "https://itunes.apple.com/" + Location + "/app/udemy-130-000-online-courses/id562413829" ;
                await Browser.OpenAsync(Url, BrowserLaunchMode.External) ;
            }

        }
    }
}

This way we can open the play store or app store from our Xamarin.Forms application. An alternate way is by using Launcher.OpenAsync() for opening play Store and App Store both. We can do this by using Dependency Services.

Dependency Service is a Static class in the Xamarin Forms namespace. It is a service locator, we can invoke native platform functionality from shared code using service locator. To use dependency services, we have to create an interface in our shared project and use that interface in all platform-specific platforms. Inherit the created interface in platform-specific projects and add a reference to that class. These codes are for creating an interface and implementing an interface to the specific platforms. The first code is for creating the interface. We will create the interface in our shared project and inherit that interface in both platforms after that we have used the method called OpenAppPlayStore which is created in the interface. The second code is for implementing the interface in the iOS platform and the third for the Android platform.

Code for creating an interface:

using System;
namespace OpenPlaystore
{
    public interface IOpenAppPlayStore
    {

        void OpenAppPlayStore() ;
    }
}

Code for iOS platform:

[assembly:Dependency(typeof(AppStoreImplementationios))]
namespace XamarinApp.iOS
{
    public class AppStoreImplementationios :IOpenAppPlayStore
    {
        public void OpenAppPlayStore()
        {
            Device.OpenUri(new Uri("https://itunes.apple.com/in/app/udemy-130-000-online-courses/id562413829 ") ) ;
        }

    }
}

Code for Android Platform:

[assembly:Dependency(typeof(AppStoreImplementationan) ) ]
namespace XamarinApp.Android
{
    public class AppStoreImplementationan :IOpenAppPlayStore
    {
        public void OpenAppPlayStore()
        {
            Device.OpenUri(new Uri("https://play.google.com/store/apps/details?id=com.udemy.android&hl=en"));
        }

    }
} 

iOS Method

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

Android Method

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

Using these ways, we can open the Play Store and App store from Xamarin.Forms Application. Here, we have created an application to open the play store and App Store from the application. For that, we have used the await Browser.OpenAsync() on the button clicked method. When the user clicks the button, it will call the Btn_Clicked method and then check for device and then call await Browser.OpenAsync (). This is one way to open the Play Store and App store another way is to use Launcher.OpenAsync (). We have achieved this by using dependency services. We have seen how to use dependency services, first of all, we have created an interface named IOpenAppPlayStore in a shared project and create OpenAppPlayStore() method and implement that interface in both android and iOS platform. In the Android platform, we have created class AppStoreImplementationan and AppStoreImplementationios for the iOS platform. In both platforms we have inherited interface, add assembly, and use a method from interface created in a shared project.

Conclusion

In this blog, we have understood how to open the Play Store and App Store from our Xamarin.Forms Application. We have seen two ways to open the Play Store and App Store. We have created one application in which we built one button and when the user clicks the button it will call the button clicked method from code behind file and call Browse.OpenAsync(). This is one-way alternate way to open the play store and App store is to use Launcher.OpenAsync() method. To use this method, we have used dependency services, and also seen how to use dependency services. We have created an interface and implemented that interface on both platforms.