[英]Xamarin Forms: How to check if GPS is on or off in Xamarin ios app?
每当我打开我的应用程序时,我都需要检查 GPS 是否打开。 如果 GPS 关闭,我需要将用户重定向到位置设置页面。 我已经使用如下依赖服务完成了 android 部分。
ILoc设置
public interface ILocSettings
{
void OpenSettings();
bool isGpsAvailable();
}
安卓实现
[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.ActionLocationSourceSettings);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
}
最后来自如下调用的共享项目:
//For checking the GPS Status
bool gpsStatus = DependencyService.Get<ILocSettings>().isGpsAvailable();
//For opening the location settings
DependencyService.Get<ILocSettings>().OpenSettings();
对于 ios,我如何才能执行相同的功能? 我试过如下:
[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));
}
}
}
在 ios 模拟器上打开位置设置页面,但不知道如何查看 GPS 状态。
更新1
我已经尝试了CLLocationManager
代码,但它没有按预期工作。 即使位置关闭或打开,它也始终返回 true。
OpenSettings() 函数代码( UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
)也没有按预期工作,它正在重定向到其他页面,如果 GPS 关闭,我需要打开位置设置页面.
另外,我正在请求位置许可,如下所示:
var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
在android 中,请求位置权限,但在 ios 中,没有请求权限。
更新2
我已经尝试了新的代码并且总是将错误值作为 GPS 状态。 我在info.plist
上添加了所有位置权限,如下所示:
但是在运行应用程序时不会询问位置权限(甚至一次)。 我试过Permissions.LocationWhenInUse
而不是Permissions.LocationAlways
,但没有运气。
更新 3
以下是我检查位置权限、检查 GPS 状态和打开设置的完整流程。 权限状态值始终为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();
}
}
我已经尝试了以下 2 个代码来请求或检查权限。 在这两种情况下,状态值都是 Disabled。 如果我卸载应用程序并重新安装它,得到相同的状态并且不显示任何权限弹出窗口。
var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
与安卓系统不同,iOS可以单独设置GPS开关,只能获取位置服务是否开启的状态。 剩下的具体定位方法就留给iOS系统去选择了。
一开始,我们需要看看iOS中location的状态:
CLAuthorizationStatus 枚举
UIApplicationOpenSettingsURLString :用于创建可以传递给 openURL: 方法的 URL。 当您打开根据该字符串构建的 URL 时,系统会启动“设置”应用程序并显示该应用程序的自定义设置(如果有)。
从现在开始,iOS 只支持这种方式来显示应用程序的自定义设置。 有两个有用的讨论,你可以看看。 iOS10如何跳转到系统设置的定位服务? 和打开位置设置在 ios 11 Objective c 中不起作用? .
如果它重定向到其他页面,如下所示:
这意味着您的应用程序在安装应用程序后不会对位置服务进行任何设置。 因此,您无需打开设置页面,因为它不会在您的应用程序设置页面下方显示位置服务。 现在CLAuthorizationStatus
应该是NotDetermined
。 您可以使用CLLocationManager.RequestWhenInUseAuthorization来请求权限,位置服务的弹出窗口将显示给客户在应用程序内选择。
如果客户第一次选择不允许,这意味着下次打开应用程序检查将显示拒绝状态的位置服务。 现在您需要使用UIApplicationOpenSettingsURLString打开设置页面,并会在应用的自定义设置列表中看到位置服务。
最后LocationShare的最终代码如下:
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));
}
}
同样,如果CLAuthorizationStatus被Denied
(与 Forms 中的status == PermissionStatus.Unknown
相同),以下代码在 Forms 中将不起作用。
var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
它仅在CLAuthorizationStatus为NotDetermined
时NotDetermined
。 并且您最好请求Permissions.LocationWhenInUse
而不是Permissions.LocationAlways
,这应该是更好的推荐选项。
============================更新#2================== ==============
我修改了上面的代码,你会看到如果CLLocationManager.LocationServicesEnabled
为false,我们只能要求用户重定向到系统设置页面手动开启服务。 因为从 iOS 10 开始,iOS 不支持从非系统应用程序导航到系统设置页面。
============================更新#3================== ====================
如果启用定位服务,当使用UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
方法你会看到类似的截图如下:
Loaction 选项将显示在列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.