Skip to main content
 首页 » 编程设计

swift之iOS 8 Swift 将用户直接从警报对话框发送到位置服务

2024年01月01日55lovecherry

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

这是当前代码(位于 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)。

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

因此,将作为 Information 属性列表下的条目添加到 plist 中:

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

如果您对位置使用全时权限级别,则为:

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

在 iOS8 中使用定位服务时,这两者都是必需的,但我看到的大多数教程都没有为字符串添加值,这最终成为对我来说最接近的解决方案。

字符串可以是您想要说服用户在应用中打开其位置设置的任何内容。这样做会通过警报对话框的操作系统系统消息版本,因此您不能对其进行太多自定义(即标题中的自定义文本、按钮的自定义文本等),但您至少可以直接发送给用户到正确的地方。

一个对我来说不是问题的警告是,您的字符串显示在对用户的初始权限请求(访问他们的位置数据)和任何基于设备关闭的位置的持续警报中.