ios 如何再次提示用户打开定位服务...

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5998465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:55:06  来源:igfitidea点击:

How to prompt user to turn on Location Services...again

iphoneioscllocationmanager

提问by toofah

I want to have the same functionality as the Map app, where user is prompted every time they press the 'current location' button to turn on their Location Services if they are off:

我希望拥有与地图应用程序相同的功能,用户每次按下“当前位置”按钮时都会收到提示,以打开他们的位置服务(如果它们已关闭):

  • Turn off location services
  • User presses 'getCurrentLocation' button
  • App tries to get location using CLLocationManager
  • User gets 'Turn On Location Services..." message that shows "Settings" and "Cancel" buttons.
  • User taps 'Cancel'
  • User presses ''getCurrentLocation' button again
  • App tries to get location using CLLocationManager again
  • User does not get 'Turn On Location Services..." message any more
  • 关闭定位服务
  • 用户按下“getCurrentLocation”按钮
  • 应用尝试使用 CLLocationManager 获取位置
  • 用户收到“打开定位服务...”消息,显示“设置”和“取消”按钮。
  • 用户点击“取消”
  • 用户再次按下“getCurrentLocation”按钮
  • 应用尝试再次使用 CLLocationManager 获取位置
  • 用户不再收到“打开定位服务...”消息

In the Map app, the user gets "Turn On Location Services..." message every time. How can I get my app to do the same? I made user I am using a new instance of CLLocationManager, in case that was the problem, but it was not. I can't see any settings that would affect this.

在地图应用程序中,用户每次都会收到“打开定位服务...”消息。我怎样才能让我的应用程序做同样的事情?我让用户我正在使用 CLLocationManager 的新实例,以防出现问题,但事实并非如此。我看不到任何会影响这一点的设置。

If I make my own Alert I cannot get the same 'Settings' button functionality. Also, I don't want the user to see multiple Alerts that look the same.

如果我制作自己的警报,则无法获得相同的“设置”按钮功能。此外,我不希望用户看到多个看起来相同的警报。

Any ideas?

有任何想法吗?

回答by Aaron Wasserman

New in iOS 8 there is a constant called UIApplicationOpenSettingsURLString.

iOS 8 中新增了一个名为UIApplicationOpenSettingsURLString.

From the "What's new in iOS"document under UIKit is the line:

UIKit 下的“iOS 中的新功能”文档中有一行:

You can take the user directly to your app-related settings in the Settings app. Pass the UIApplicationOpenSettingsURLStringconstant to the openURL: method of the UIApplication class.

您可以将用户直接带到“设置”应用中与您的应用相关的设置。将UIApplicationOpenSettingsURLString常量传递给 UIApplication 类的 openURL: 方法。

From Apple's documentation:

来自苹果的文档

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.

UIApplicationOpenSettingsURLString

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

You can pass this into the UIApplication openURL: method. It might look something like:

您可以将其传递给 UIApplication openURL: 方法。它可能看起来像:

NSURL *settings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

if ([[UIApplication sharedApplication] canOpenURL:settings])
    [[UIApplication sharedApplication] openURL:settings];

回答by Jacob White

If you want to point the user back to the Location Services screen in the Settings app, you can do so by sending them to a special URL, like so:

如果您想将用户指向设置应用程序中的位置服务屏幕,您可以通过将他们发送到一个特殊的 URL 来实现,如下所示:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"prefs:root=LOCATION_SERVICES"]];

回答by PeyloW

You can query the shared CLLocationManagerinstance if the location service is enabled. The correctway is to respect the users choice to disable location services.

CLLocationManager如果启用了位置服务,您可以查询共享实例。在正确的办法是尊重用户选择禁用位置服务。

But if you want to, just start the location service anyway and the user will be prompted to start it again. If the user opts in on the request locations will begin to be reported on your delegate as usual. If the user instead denies your request you will get a failure callback to the locationManager:didFailWithError:delegate method. The error will have an error code of kCLErrorDenied.

但是,如果您愿意,无论如何只需启动位置服务,系统将提示用户再次启动它。如果用户选择加入请求位置,将像往常一样开始向您的委托报告。如果用户拒绝您的请求,您将收到locationManager:didFailWithError:委托方法的失败回调。该错误的错误代码为kCLErrorDenied

I would strongly discourage you from doing this, but you can try to start the service again if the user says no, and the user will be asked again. Most users will hate you for it though.

我强烈建议您不要这样做,但是如果用户拒绝,您可以尝试再次启动该服务,并且将再次询问用户。不过,大多数用户会因此而讨厌您。