Xamarin表格:如何检查Xamarin ios应用程序中的GPS是否打开或关闭? [英] Xamarin Forms: How to check if GPS is on or off in Xamarin ios app?

查看:157
本文介绍了Xamarin表格:如何检查Xamarin ios应用程序中的GPS是否打开或关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我打开我的应用程序时,都需要检查GPS处于打开还是关闭状态.如果GPS关闭,则需要将用户重定向到位置设置页面.我已经使用

但是在运行应用程序时(甚至不是一次)都不会询问位置许可.我尝试使用 Permissions.LocationWhenInUse 代替 Permissions.LocationAlways ,但是没有运气.

更新3

以下是我检查位置权限,检查GPS状态和打开设置的完整流程.权限状态值始终为 Disabled .

 //请求权限,如下所示:var status = await Permissions.RequestAsync< Permissions.LocationAlways>();如果(状态== PermissionStatus.Granted){//然后检查GPS状态,如下所示bool gpsStatus = DependencyService.Get< ILocSettings>().isGpsAvailable();如果(!gpsStatus){//在此处向用户显示一条消息以共享GPS//如果用户授予GPS共享权限,则打开位置设置页面,如下所示:DependencyService.Get< ILocSettings>().OpenSettings();}} 

我尝试了以下2个代码来请求或检查权限.在这两种情况下,状态值均为禁用".如果我卸载该应用程序并再次重新安装,则状态相同,并且未显示任何权限弹出窗口.

  var status =等待Permissions.RequestAsync< Permissions.LocationAlways>();var status =等待Permissions.CheckStatusAsync< Permissions.LocationWhenInUse>(); 

解决方案

不同于 Android系统,iOS可以分别设置 GPS 开关,并且只能获取状态定位服务是否已打开.其余的特定定位方法将留给iOS系统选择.

一开始,我们需要查看iOS中的位置状态:

CLAuthorizationStatus枚举

UIApplicationOpenSettingsURLString :用于创建可传递给openURL:方法的URL.当您打开根据此字符串构建的URL时,系统将启动设置"应用程序,并显示该应用程序的自定义设置(如果有).

从现在开始,iOS仅支持这种方式来显示应用程序的自定义设置.有两个有用的讨论,您可以看一下.

这意味着您的应用程序在安装后不会对位置服务进行任何设置.因此,您无需打开设置页面,因为它不会在您应用程序的设置页面下方显示位置服务.现在, CLAuthorizationStatus 应该为 NotDetermined .您可以使用 CLLocationManager.RequestWhenInUseAuthorization 来请求权限,定位服务弹出窗口将显示,供客户在应用内选择.

如果客户第一次选择不允许,则意味着下次打开该应用以检查将显示已拒绝状态的定位服务.现在,您需要使用 UIApplicationOpenSettingsURLString 打开设置页面,并在应用程序的自定义设置列表中看到位置服务.

最后, LocationShare 的最终代码如下:

 公共类LocationShare:ILocSettings{公共布尔isGpsAvailable(){布尔值=假;如果(CLLocationManager.LocationServicesEnabled){if(CLLocationManager.Status == CLAuthorizationStatus.Authorized || CLLocationManager.Status == CLAuthorizationStatus.AuthorizedAlways || CLLocationManager.Status == CLAuthorizationStatus.AuthorizedWhenInUse){//使能够值= true;}否则,如果(CLLocationManager.Status == CLAuthorizationStatus.Denied){值=假;打开设置();}别的{值=假;RequestRuntime();}}别的{//位置服务为假值=假;//要求用户打开系统设置页面以手动将其打开.}返回值;}公共无效RequestRuntime(){CLLocationManager cLLocationManager =新的CLLocationManager();cLLocationManager.RequestWhenInUseAuthorization();}公共无效的OpenSettings(){UIApplication.SharedApplication.OpenUrl(新的NSUrl(UIApplication.OpenSettingsUrlString));}} 

类似地,如果 CLAuthorizationStatus 拒绝(与表单中的 status == PermissionStatus.Unknown 相同),以下代码将无法在Forms中使用.

  var status =等待Permissions.RequestAsync< Permissions.LocationAlways>(); 

仅当 CLAuthorizationStatus NotDetermined 时有效.而且最好还是请求 Permissions.LocationWhenInUse 而不是 Permissions.LocationAlways ,这应该是更好的建议选项.

===========================更新#2 ================================

我已经修改了上面的代码,您会看到如果 CLLocationManager.LocationServicesEnabled 为false,我们只能要求用户重定向到系统设置页面以手动打开服务.因为从iOS 10开始,iOS不支持从非系统应用导航到系统设置页面.

===========================更新#3 ======================================

如果启用了位置服务,则在使用 UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString)); 方法时,您将看到类似的屏幕截图,如下所示:

运动"选项将显示在列表中.

Whenever I am opening my app I need to check the GPS is on or off. If the GPS is off, I need to redirect the user to the location settings page. I have done the android part using the dependency service like below.

ILocSettings

public interface ILocSettings
{
    void OpenSettings();

    bool isGpsAvailable();
}

Android implementation

[assembly: Dependency(typeof(LocationShare))]
namespace Projectname.Droid.Services 
{
    public class LocationShare : ILocSettings
    {
        public bool isGpsAvailable()
        {
            bool value = false;
            Android.Locations.LocationManager manager = (Android.Locations.LocationManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.LocationService);
            if (!manager.IsProviderEnabled(Android.Locations.LocationManager.GpsProvider))
            {
                //gps disable
                value = false;
            }
            else
            {
                //Gps enable
                value = true;
            }
            return value;
        }

        public void OpenSettings()
        {
            Intent intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocat‌​ionSourceSettings);
            intent.AddFlags(ActivityFlags.NewTask);
            Android.App.Application.Context.StartActivity(intent);
        }
    }
}

Finally from the shared project called like below:

//For checking the GPS Status
bool gpsStatus = DependencyService.Get<ILocSettings>().isGpsAvailable();
//For opening the location settings 
DependencyService.Get<ILocSettings>().OpenSettings();

For ios how I can I do the same features? I tried like below:

[assembly: Dependency(typeof(LocationShare))]
namespace Projectname.iOS.Serivces
{
    class LocationShare : ILocSettings
    {
        public bool isGpsAvailable()
        {
            //how to check the GPS is on or off here
        }

        public void OpenSettings()
        {
            UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
        }
    }
}

Location settings page opening on ios simulators, but don't know how to check the GPS status.

Update1

I have tried the CLLocationManager code and it is not working as expected. It returns true always even if the location is off or on.

OpenSettings() function code (UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));) is also not working as expected, it is redirecting to some other page, I need to open the location settings page if the GPS is off.

Also, I am requesting location permission like below:

var status = await Permissions.RequestAsync<Permissions.LocationAlways>();

In android, location permission is asking, but in ios, no permissions are asking.

Update2

I have tried the new codes and getting false value always as GPS status. I have added all the location permission on the info.plist like below:

But location permission is not asking when running the app (not even a single time). I have tried Permissions.LocationWhenInUse instead of Permissions.LocationAlways, but no luck.

Update 3

Following is my complete flow for checking location permission, checking GPS status, and open settings. The permission status value is always Disabled.

//Requesting permission like below:
var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
if (status == PermissionStatus.Granted)
{
    //Then checking the GPS state like below
    bool gpsStatus = DependencyService.Get<ILocSettings>().isGpsAvailable();
    if (!gpsStatus)
    {
        //show a message to user here for sharing the GPS
        //If user granted GPS Sharing, opening the location settings page like below:
        DependencyService.Get<ILocSettings>().OpenSettings();
    }
}

I have tried the below 2 codes for requesting or checking permission. In both cases, the status value is Disabled. If I uninstall the app and reinstall it again, getting the same status and not showing any permission pop-up window.

var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

解决方案

Unlike the Android system, iOS can set the GPS switch separately, and can only get the status of whether the location service is turned on. The rest of the specific positioning method will be left to the iOS system to choose.

At the beginning, we need to have a look at the status of location in iOS:

CLAuthorizationStatus Enum

UIApplicationOpenSettingsURLString: Used to create a URL that you can pass to the openURL: method. When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any.

From now, iOS only support this way to displays the app’s custom settings. There are two helpful discussion, you could have a look. How to jump to system setting's location service on iOS10? and Open Location Settings Not working in ios 11 Objective c?.

If it is redirecting to some other page as follows:

That means your app not do any settings about the location service after installing the app . Therefore, you not need to open the setting page, because it will not show the location service bellow the setting page of your app. Now the CLAuthorizationStatus should be NotDetermined. You could use CLLocationManager.RequestWhenInUseAuthorization to request the permission, the popup window of location service will show for customer to choose inside the app.

If customer select Don't Allow first time, that means next time opening the app to check the location service that will show Denied status. Now you will need to use UIApplicationOpenSettingsURLString to open the settings page and will see the location service inside the app’s custom settings list.

At last, the final code of LocationShare is as follows:

public class LocationShare : ILocSettings
{
    public bool isGpsAvailable()
    {
        bool value = false;

        if ( CLLocationManager.LocationServicesEnabled )
        {
            if(CLLocationManager.Status == CLAuthorizationStatus.Authorized || CLLocationManager.Status == CLAuthorizationStatus.AuthorizedAlways || CLLocationManager.Status == CLAuthorizationStatus.AuthorizedWhenInUse)
            {//enable
                value = true;
            }
            else if (CLLocationManager.Status == CLAuthorizationStatus.Denied)
            {
                value = false;
                OpenSettings();
            }
            else{
                value = false;
                RequestRuntime();
            }
        }
        else
        {
            //location service false
            value = false;
            //ask user to open system setting page to turn on it manually.
        }
        return value;
    }


    public void RequestRuntime()
    {
        CLLocationManager cLLocationManager = new CLLocationManager();
        cLLocationManager.RequestWhenInUseAuthorization();
    }

    public void OpenSettings()
    {

        UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
    }
}

Similarly, if CLAuthorizationStatus is Denied (the same as status == PermissionStatus.Unknown in Forms), the following code will not work in Forms.

var status = await Permissions.RequestAsync<Permissions.LocationAlways>();

It only works when CLAuthorizationStatus is NotDetermined. And you'd better request Permissions.LocationWhenInUse instead of Permissions.LocationAlways, this should be the better recommanded option.

============================Update #2================================

I have modified the above code, and you will see that if CLLocationManager.LocationServicesEnabled is false, we only can ask user to redirect to the system setting page to turn on the service manually. Because from iOS 10, iOS not supports to navigate to system setting page from non-system app.

============================Update #3======================================

If location service is enabled, when using UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString)); method you will see the similar screenshot as follows:

The Loaction option will show in the list.

这篇关于Xamarin表格:如何检查Xamarin ios应用程序中的GPS是否打开或关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆