位置服务在iOS 11中不起作用

时间:2017-06-07 23:01:59

标签: ios swift iphone ios11 location-services

我刚刚使用iOS 11 SDK重建了我的应用,试图删除现在总是出现的blue banner。我认为 - “太棒了,那很有效”,只是发现定位服务现在根本不起作用。

用于iOS 10的应用程序 - 有没有人听到过什么?

8 个答案:

答案 0 :(得分:152)

似乎苹果增加了另一项隐私功能。用户现在可以覆盖我们的requestAlwaysAuthorization并将其降级为requestWhenInUseAuthorization - 这意味着作为开发人员,我们现在必须在Info.plist

中提供这两种说明

我发现他们添加了新密钥NSLocationAlwaysAndWhenInUseUsageDescription

/*
*      Either the NSLocationAlwaysAndWhenInUseUsageDescription key or both the
*      NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription
*      keys must be specified in your Info.plist; otherwise, this method will do
*      nothing, as your app will be assumed not to support Always authorization.
*/

然而,在使用这个新密钥时 - 位置服务仍然无效,进一步搜索后我发现这个宝石混入了所有额外的调试信息:

  

此应用尝试在没有使用说明的情况下访问隐私敏感数据。应用程序的Info.plist必须包含NSLocationAlwaysAndWhenInUseUsageDescription和NSLocationWhenInUseUsageDescription键,并使用字符串值向用户解释应用程序如何使用此数据

这与我在更新的CLLocationManager.h文件中找到的评论直接相矛盾。所以我创造了一个雷达。

好消息,如果你按照调试控制台的建议,IE。添加新密钥NSLocationAlwaysAndWhenInUseUsageDescription和其中一个旧密钥NSLocationWhenInUseUsageDescription,位置服务将重新开始工作。

答案 1 :(得分:37)

只是添加修复此步骤的步骤:

有两种方法:

A)简单方法:  选择你的Info.plist文件,添加属性,注意它们以PRIVCY而不是LOCATION开头...因此,这些变量的确切名称以" Privacy - Location ..."开头。等,在这里添加每个,并描述用户将如何在警告上看到这一点。

B)艰难/有趣/程序化的方式(我更喜欢这种方式):

右键单击您的应用的Info.plist,然后选择"查看源代码",您应该以XML格式查看所有内容,

遵循其他......格式,并按如下方式添加这些属性:

<key>NSLocationAlwaysUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app uses your Microphone to allow Voice over IP communication with the Program Admin system</string>

保存,然后右键单击info.plist文件,然后选择属性列表,这应该将文件重新显示回默认视图。

编辑:

另一位成员要求代码,这里是:

1)在你的.H文件中,添加:

@property (strong, nonatomic) CLLocationManager *LocationManager;

2)在你的.M文件中添加ViewDidAppear()函数:

_LocationManager = [[CLLocationManager alloc] init];
[_LocationManager setDelegate:self];
_LocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_LocationManager.pausesLocationUpdatesAutomatically = NO;
[_LocationManager requestAlwaysAuthorization];

_LocationManager.headingFilter = 5;
_LocationManager.distanceFilter = 0;

[_LocationManager startUpdatingLocation];
[_LocationManager startUpdatingHeading];

这对我来说很好用,希望代码对你也有用。

此致

Heider的

答案 2 :(得分:20)

在我发现的iOS11下工作,Info.plist在Info.plist中至少需要NSLocationAlwaysAndWhenInUseUsageDescription:

enter image description here

当您的应用多语言时,您的字符串的本地化版本需要所有三个键,而这篇文章中提到的{strong> requestAlwaysAuthorization()locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) 将无声地失败。

以德语翻译为例:

enter image description here

希望这可以节省您绊倒的时间。

答案 3 :(得分:15)

在Swift 4.0.3中工作

   <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
   <string>Description</string>

   <key>NSLocationAlwaysUsageDescription</key>
   <string>Will you allow this app to always know your location?</string>

   <key>NSLocationWhenInUseUsageDescription</key>
   <string>Do you allow this app to know your current location?</string>  

答案 4 :(得分:10)

请按照以下步骤操作:

我遇到了与需要“始终授权”的应用相同的问题,并按照以下步骤解决了这个问题:

1。 NSLocationWhenInUseUsageDescription密钥添加到Info.plist

2。 添加 NSLocationAlwaysAndWhenInUseUsageDescriptionInfo.plist

3。 添加 NSLocationAlwaysUsageDescriptionInfo.plist(支持&lt; iOS 11)

4. 致电 requestWhenInUseAuthorization() 之前 requestAlwaysAuthorization(

您无法在requestWhenInUseAuthorization()之前执行requestAlwaysAuthorization()。您必须升级到该权限级别。完成这些更改后,位置更新再次开始正常工作。

更多细节可以在这里找到:

https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/requesting_always_authorization

答案 5 :(得分:6)

比抱歉更安全.. 在iOS 11中:添加以下内容,你很好。

<key>NSLocationWhenInUseUsageDescription</key>
<string>Description</string>

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>Description</string>

答案 6 :(得分:1)

斯威夫特:3 我遇到了同样的问题。我完全搞砸了找到解决方案。这是我如何解决问题。

步骤1:项目文件&gt;能力&gt;背景模式&gt;选择位置更新

第2步:  将NSLocationWhenInUseUsageDescription,NSLocationAlwaysAndWhenInUseUsageDescription键添加到 Info.plist

第3步:

manager.pausesLocationUpdatesAutomatically = false
manager.allowsBackgroundLocationUpdates = true

答案 7 :(得分:0)

iOS 12.2 上使用 Swift 5

进行了测试

步骤1。您必须在plist文件中添加以下隐私权限

<key>NSLocationWhenInUseUsageDescription</key>
<string>Description</string>

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>Description</string>

第2步。确保您具有快速代码以获取当前位置

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    // MARK: Variables declearations
    @IBOutlet weak var mapView: MKMapView!
    var locationManager: CLLocationManager!

    // MARK: View Controller life cycle methods
    override func viewDidLoad() {
        super.viewDidLoad()
        //TODO: Make user you must add following three privacy permissions in plist
        //NSLocationWhenInUseUsageDescription
        //NSLocationAlwaysAndWhenInUseUsageDescription
        //NSLocationAlwaysUsageDescription

        getCurrentLocation()
    }

    func getCurrentLocation()
    {
        if (CLLocationManager.locationServicesEnabled())
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.startUpdatingLocation()
        }
    }

    // MARK: Location Manager Delegate methods
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let locationsObj = locations.last! as CLLocation
        print("Current location lat-long is = \(locationsObj.coordinate.latitude) \(locationsObj.coordinate.longitude)")
        showOnMap(location: locationsObj)
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Get Location failed")
    }

    func showOnMap(location: CLLocation )
    {
        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        mapView.setRegion(region, animated: true)
    }
}