How to prompt user to turn on Location Services…again

ε祈祈猫儿з 提交于 2019-11-27 18:50:59
Aaron Wasserman

New in iOS 8 there is a constant called UIApplicationOpenSettingsURLString.

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

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

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.

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

NSURL *settings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

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

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:

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

You can query the shared CLLocationManager instance if the location service is enabled. The correct way is to respect the users choice to disable location services.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!