iOS 8 Swift从“警报”对话框直接将用户发送到位置服务 [英] iOS 8 Swift sending user directly to location services from an Alert dialog

查看:163
本文介绍了iOS 8 Swift从“警报”对话框直接将用户发送到位置服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户关闭位置服务时,我想提示使用UIAlertController打开它们。警报控制器中的一个选项应该直接将它们带到位置服务(对于操作系统,而不是应用程序)。现在,我已经能够将用户发送给我的特定应用程序的权限,但不能用于整体操作系统位置设置。有没有办法将用户从UIAlertController直接发送到OS位置设置?

When a user has location services turned off, I want to prompt to turn them on with a UIAlertController. One of the options in the Alert Controller should take them directly to location services (for the OS, not the app). Right now, I've gotten as far as being able to send the user to permissions for my specific app, but not for the overall OS location settings. Is there a way to send the user directly to the OS Location settings from the UIAlertController?

这是当前代码(位于viewDidAppear中):

Here is the current code (located in viewDidAppear):

if (CLLocationManager.locationServicesEnabled())
{
    daMap.delegate = self
    daMap.mapType = MKMapType.Satellite
    daMap.showsUserLocation = true
    // do some other things...
} else {
    let alertController = UIAlertController(
        title: "We Can't Get Your Location",
        message: "Turn on location services on your device.",
        preferredStyle: .Alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    alertController.addAction(cancelAction)

    let openAction = UIAlertAction(title: "Location Settings", style: .Default) { (action) in
        if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
            UIApplication.sharedApplication().openURL(url)
        }
    }
    alertController.addAction(openAction)

    self.presentViewController(alertController, animated: true, completion: nil)
}


推荐答案

正如马特评论的那样,这个问题的答案是不。出于某种原因,对此的具体答案在网上更难找到,可能是因为没有解决方案(至少没有UIAlertController)。

As "Matt" commented, the answer to this questions is "NO". For some reason, that specific answer to this was more difficult to find on the web, probably because of there not being a solution (at least not with a UIAlertController).

然而,我发现最可接受的替代方法是将NSLocationWhenInUseUsageDescription字符串添加到plist文件(无论如何都是必需的),并给出值你想要的消息。

However, what I found to be the most acceptable alternative is the adding of the NSLocationWhenInUseUsageDescription string to the plist file (which is required anyway), and giving the value of the message you want.

因此,在信息属性列表下添加plist作为条目:

So, add to the plist as an entry under the Information property list:

key: NSLocationWhenInUseUsage
String value: Turn on location services on your device.

如果您使用一直以来的位置权限级别,则为:

If you use the all-the-time permission level for location, it is this:

key: NSLocationAlwaysUsageDescription
String value: Turn on location services on your device.

使用位置服务时iOS8中都需要这些,但我见过的大多数教程都没有t为字符串添加了值,这最终成为我最接近的解决方案。

Both of these are required in iOS8 when using location services, but most tutorials I've seen haven't added values for the strings, which ended up being the closest solution for me.

字符串可以是任何你想说服用户开启其位置设置的字符串在应用程序中。这样做是通过警告对话框的操作系统消息版本进行的,因此您无法对其进行相当多的自定义(即标题中的自定义文本,按钮的自定义文本等),但您至少可以直接发送给用户到正确的地方。

And the strings can be whatever you want to convince the user to turn on their location settings in the app. Doing it this way goes through the OS system message version of the alert dialog, so you can't customize it quite as much (i.e. custom text in the title, custom text for the buttons, etc.), but you at least get to send to the user directly to the right place.

一个警告,对我来说不是问题,是你的字符串显示在对用户的初始许可请求(访问他们的位置数据)和根据设备关闭位置的任何持续警报。

One caveat, which wasn't an issue for me, is that your string is displayed in both the initial permission request to the user (to access their location data) and any ongoing alerts based on location being turned off on the device.

这篇关于iOS 8 Swift从“警报”对话框直接将用户发送到位置服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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