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

Wil*_*rge 80 iphone ios swift location-services ios11

我刚用iOS 11 SDK重建了我的应用程序,试图删除blue banner现在总是出现的应用程序.我想 - "太棒了,有用",却发现定位服务现在根本不起作用.

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

Wil*_*rge 154

苹果似乎又添加了另一项隐私功能.用户现在能够覆盖我们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.
*/
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

  • 不幸的是,用户现在得到了三个答案的对话,其中由于用户不耐烦而最有可能被击中的是最佳选择:"仅在使用应用程序时".这将导致需要始终使用的应用程序无用,除非用户经历了之后再次更改设置的所有麻烦.当应用程序要求时,至少"始终允许"应该是最佳选择! (7认同)
  • 我面临着非常相似的情况.只有我在iOS 10.2中测试,并且我在我的Info.plist中添加了"NSLocationAlwaysUsageDescription",这是我需要的权限.但我在控制台中收到上述消息,说我还需要添加`NSLocationWhenInUseUsageDescription`. (2认同)

Hei*_*ati 39

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

2种方法:

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>
Run Code Online (Sandbox Code Playgroud)

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

编辑:

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

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

@property (strong, nonatomic) CLLocationManager *LocationManager;
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

这对我来说很好,希望代码也适合你.

问候

海德


iDo*_*Doc 20

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

在此输入图像描述

当您的应用程序是多语言时,奇怪的是,您的字符串的本地化版本需要此帖子中提到的所有三个键,requestAlwaysAuthorization()并且locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) 将无声地失败.

拍摄显示德语翻译为例:

在此输入图像描述

希望这可以节省你绊倒的时间.


Kes*_*era 16

在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>  
Run Code Online (Sandbox Code Playgroud)


mhi*_*it0 12

跟着这些步骤:

我遇到了一个需要"始终授权"的应用程序遇到了同样的问题,并通过以下步骤解决了这个问题:

1. 添加 NSLocationWhenInUseUsageDescription密钥Info.plist

2. 添加 NSLocationAlwaysAndWhenInUseUsageDescriptionInfo.plist

3. 添加 NSLocationAlwaysUsageDescriptionInfo.plist(支持<iOS 11)

4. 之前致电 )requestWhenInUseAuthorization() requestAlwaysAuthorization(

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

更多详情可在这找到:

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


Abh*_*edi 6

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

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

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

<key>NSLocationAlwaysUsageDescription</key>
<string>Description</string>
Run Code Online (Sandbox Code Playgroud)


swi*_*Boy 5

使用Swift 5iOS 12.2上测试

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

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Application requires user’s location for better user experience.</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>Application requires user’s location for better user experience.</string>

<key>NSLocationWhenInUseUsageDescription</key>
<string>Application requires user’s location for better user experience.</string>
Run Code Online (Sandbox Code Playgroud)

第 2 步。确保您有以下 swift 代码以获取当前位置

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)
    }
}
Run Code Online (Sandbox Code Playgroud)