简体   繁体   English

Xamarin.Forms 如何在 Android 和 iOS 上添加应用评​​级?

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

在 Xamarin.Forms 应用程序(直接连接到 Play 商店或 App Store 的默认星形表格)上添加应用评​​级的最佳/最简单选项是哪个?

Edit : I've created an nuget package for this, you can download from Here or check the GitHub repo .编辑:我为此创建了一个 nuget 包,您可以从这里下载或查看GitHub 存储库

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.在 Android 上,您必须打开 PlayStore 才能对应用程序进行评分,在 iOS 上,您可以在应用程序内执行此操作,但仅限 iOS 10 及更高版本。

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

在 Android 上(在9.0测试),我必须在FabriBertani 的解决方案中添加此标志:

activity.StartActivity(intent);

The Store Review Nuget plugin has been updated to v3 recently thanks to the new (August 2020) Android native Play Core library so that it displays an In-App Review on both iOS & Android using just one line of code:借助新的(2020 年 8 月)Android 原生 Play Core 库, Store Review Nuget 插件最近已更新到 v3,因此它仅使用一行代码即可在 iOS 和 Android 上显示应用内评论:

await CrossStoreReview.Current.RequestReview(false);

So now, you don't have to worry about opening the url of the app in the external store or a web view, it will be shown directly in the app.所以现在,您不必担心在外部商店或网络视图中打开应用程序的 url,它将直接显示在应用程序中。 You can see more details on this Microsoft Blog您可以在 此 Microsoft 博客上查看更多详细信息

Just remember to add the necessary code in the proguard file mentioned in the README of the Nuget's GitHub repository只需记住在 Nuget 的 GitHub 存储库README 中提到的 proguard 文件中添加必要的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM