icons

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

iFour Team - January 19, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

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

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


            
            
            
            
        
            



        
    
 

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() ;
    }
}

One Stop Solution for Xamarin Mobile App Development ? Your Search ends here.

 

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.

Technology Stacks

Technology that meets your business requirements

Planning a cutting-edge technology software solution? Our team can assist you to handle any technology challenge. Our custom software development team has great expertise in most emerging technologies. At iFour, our major concern is towards driving acute flexibility in your custom software development. For the entire software development life-cycle, we implement any type of workflow requested by the client. We also provide a set of distinct flexible engagement models for our clients to select the most optimal solution for their business. We assist our customers to get the required edge that is needed to take their present business to next level by delivering various out-reaching technologies.

Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo

Our Global Presence

Get advanced technology that will help you find the
right answers for your every business need.

Get in touch

Need a Consultation?

Drop us a line! We are here to answer your questions 24/7.