如何查看定位服务是否开启?

发布于 2024-10-16 06:19:25 字数 70 浏览 2 评论 0 原文

如何检查用户是否关闭了定位服务?

这样我就可以提示他/她打开它才能使用我的应用程序。

谢谢 !

How can I check if the user has turned off Location Services ?

So that I can prompt him/her to turn it On in order to use my app.

Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巷雨优美回忆 2024-10-23 06:19:25

CLLocationManager 提供了类方法来确定位置服务的可用性:

- (BOOL)locationServicesEnabled(适用于

+ (BOOL)locationServicesEnabled(适用于 iOS) 4.0 及更高版本)

+ (CLAuthorizationStatus)authorizationStatus(适用于 iOS 4.2+)

(及其他版本,请参阅文档)

The CLLocationManager provides class methods to determine the availability of location services:

- (BOOL)locationServicesEnabled (for < iOS 4.0)

+ (BOOL)locationServicesEnabled (for iOS 4.0 and greater)

+ (CLAuthorizationStatus)authorizationStatus (for iOS 4.2+)

(and others, see documentation)

葮薆情 2024-10-23 06:19:25

如果您的应用程序在没有位置服务的情况下绝对无法运行,那么您可以使用应用程序的 Info.plist 将位置服务作为安装/运行应用程序的要求。您可以通过将 UIDeviceCapability 键添加到应用程序的 Info.plist 并为其指定相应的“位置服务”值减去引号来完成此操作。

以这种方式配置 Info.plist 后,如果定位服务关闭,或者设备处于飞行模式,或者其他任何因素阻止在设备上使用定位服务,iOS 将提示用户在以下情况下打开定位服务:应用程序已打开。

编辑:简短的实验似乎表明 iOS 在这种情况下不会提示用户,因此这对您来说不是一个好的解决方案。

有关详细信息,您可以查看 Apple 开发人员文档的信息属性列表关键参考部分。

If your application absolutely cannot run without Location Services, then you can make Location Services a requirement for installing/running your app using the app's Info.plist. You do this by adding the UIDeviceCapabilities key to your app's Info.plist and giving it a corresponding value of "location-services" minus the quotes.

With the Info.plist configured this way, if Location Services are turned off, or if the device is in airplane mode, or anything else is preventing the use of Location Services on the device, iOS will prompt the user to turn on Location Services when the app is opened.

EDIT: Brief experimentation seems to indicate that iOS does not prompt the user in this circumstance, so this would not be a good solution for you.

For more information, you can look at the Information Property List Key Reference section of Apple's developer documentation.

路弥 2024-10-23 06:19:25

使用下面的代码...

  if (![CLLocationManager locationServicesEnabled]) {


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
                                                    message:@"To re-enable, please go to Settings and turn on Location Service for this app."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];

}

Use the below piece of code...

  if (![CLLocationManager locationServicesEnabled]) {


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
                                                    message:@"To re-enable, please go to Settings and turn on Location Service for this app."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];

}
无妨# 2024-10-23 06:19:25

使用以下代码,即使在 iOS 8 中也可以使用。

if([CLLocationManager locationServicesEnabled]&&
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
  //...Location service is enabled
}
else
{
 //...Location service is disabled
}

Use the following code, which will work even in iOS 8.

if([CLLocationManager locationServicesEnabled]&&
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
  //...Location service is enabled
}
else
{
 //...Location service is disabled
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文