Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms how to add app rating on Android and iOS?

Which is the best/simplest option to add app rating on a Xamarin.Forms application, the default stars form directly connected to the Play Store or App Store?

like image 428
notarealgreal Avatar asked Apr 30 '19 10:04

notarealgreal


People also ask

Does Xamarin work with iOS and Android?

Xamarin. Forms is an open source mobile UI framework from Microsoft for building iOS, Android, & Windows apps with . NET from a single shared codebase.

Is Xamarin good for Android development?

Ultimately, appreneurs should use Xamarin for building mobile apps, as the platform offers an excellent means to build mobile apps without utilizing OS-specific languages like Swift or Java. The advantage of this is more efficient and comprehensive development as codebases can be shared cross-platform.


2 Answers

Edit: I've created an nuget package for this, you can download from Here or check the GitHub repo.

On Android you must open the PlayStore in order to rate the app, on iOS you can do it inside the app, but only from iOS 10 onwards.

You must implement native methods and use it via dependecy service.

Interface

public interface IAppRating
{
    void RateApp();
}

Android

public class AppRatiing : IAppRating
{
    public void RateApp()
    {
        var activity = Android.App.Application.Context;
        var url = $"market://details?id={(activity as Context)?.PackageName}";

        try
        {
            activity.PackageManager.GetPackageInfo("com.android.vending", PackageInfoFlags.Activities);
            Intent intent = new Intent(Intent.ActionView, Uri.Parse(url));

            activity.StartActivity(intent);
        }
        catch (PackageManager.NameNotFoundException ex)
        {
            // this won't happen. But catching just in case the user has downloaded the app without having Google Play installed.

            Console.WriteLine(ex.Message);
        }
        catch (ActivityNotFoundException)
        {
            // if Google Play fails to load, open the App link on the browser 

            var playStoreUrl = "https://play.google.com/store/apps/details?id=com.yourapplicationpackagename"; //Add here the url of your application on the store

            var browserIntent = new Intent(Intent.ActionView, Uri.Parse(playStoreUrl));
            browserIntent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ResetTaskIfNeeded);

            activity.StartActivity(browserIntent);
        }
    }
}

iOS

public class AppRating : IAppRating
{
    public void RateApp()
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion(10, 3))
            SKStoreReviewController.RequestReview();
        else
        {
            var storeUrl = "itms-apps://itunes.apple.com/app/YourAppId";
            var url = storeUrl + "?action=write-review";

            try
            {
                UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
            }
            catch(Exception ex)
            {
                // Here you could show an alert to the user telling that App Store was unable to launch

                Console.WriteLine(ex.Message);
            }
        }
    }
}
like image 80
FabriBertani Avatar answered Nov 10 '22 22:11

FabriBertani


On Android (tested in 9.0) I have to add this flag at FabriBertani´s solution:

activity.StartActivity(intent);
like image 33
Suplanus Avatar answered Nov 10 '22 22:11

Suplanus