c# - 如何在运行时请求 iOS 权限(相机和位置)?

标签 c# ios unity-game-engine

我正在 Unity 中制作一个应用程序,它使用手机的摄像头和用户的位置。在用户阅读了我希望他们授予权限的原因后,我希望向用户显示这些权限弹出窗口。所以按下应用程序中的“接受”按钮应该检查应用程序是否具有特定权限,如果没有:请求该权限。 所以简而言之,我需要的是: - 一种检查应用程序是否具有特定权限的方法 - 一种触发权限弹出窗口的方法 -(很高兴:)一种检查在所述弹出窗口中是否授予或拒绝权限的方法

我已经为 Android 构建了这个,因为 android 提供了这样做的方法。 谷歌搜索后,我发现当应用程序启动某个需要未授予权限的服务时,iOS 会提示权限弹出窗口。这意味着我可以简单地启动位置服务或相机来检查应用程序是否具有这些权限。不过,我认为这是一个肮脏的修复,因为它需要应用程序启动当时不需要的服务。

最佳答案

欢迎来到 StackOverflow!

您可以使用这个库:https://github.com/jamesmontemagno/PermissionsPlugin

如果要取回位置权限,可以使用这段代码:

try
{
    var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
    if (status != PermissionStatus.Granted)
    {
        if(await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
        {
            await DisplayAlert("Need location", "Gunna need that location", "OK");
        }

        var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
        //Best practice to always check that the key exists
        if(results.ContainsKey(Permission.Location))
            status = results[Permission.Location];
    }

    if (status == PermissionStatus.Granted)
    {
        var results = await CrossGeolocator.Current.GetPositionAsync(10000);
        LabelGeolocation.Text = "Lat: " + results.Latitude + " Long: " + results.Longitude;
    }
    else if(status != PermissionStatus.Unknown)
    {
        await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
    }
}
catch (Exception ex)
{

    LabelGeolocation.Text = "Error: " + ex;
}

关于c# - 如何在运行时请求 iOS 权限(相机和位置)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55356583/

相关文章:

c# - 按钮单击事件未在 ASP .Net 中的使用控件内触发

c# - 线程停止

android - 如何重定向到商店中使用旧名称的应用程序?

c# - 如何以触摸方向统一移动对象

c# - 防止 Unity 抛出有关需要专业版才能使用我未使用的特定功能的构建错误

c# - 如何在Unity中赋予相机的边缘碰撞

c# - 从问题中生成不同路径的矩阵?,排列?,组合?

c# - 致命 : Could not find component we just installed

ios - 如何在 UIView transitionWithView 之后运行代码?

ios - Autolayout layoutIfNeeded 正在打破约束