定位服务在 iOS 11 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44424092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Location Services not working in iOS 11
提问by William George
I just rebuilt my app with the iOS 11 SDK in an attempt to remove the blue banner
that is now always appearing. I thought - "Brilliant, that worked", only to discover that location services are now not working at all.
我刚刚使用 iOS 11 SDK 重建了我的应用程序,试图删除blue banner
现在总是出现的应用程序。我想 - “太棒了,那行得通”,结果发现定位服务现在根本不起作用。
The application used to work with iOS 10 - Has anybody heard anything?
该应用程序曾经适用于 iOS 10 - 有人听说过吗?
回答by William George
It would appear that apple have added yet another privacy feature. The user is now able to override our requestAlwaysAuthorization
and downgrade it to requestWhenInUseAuthorization
- Which means as a developer we now have to supply both descriptions in the Info.plist
苹果似乎还添加了另一个隐私功能。用户现在可以覆盖我们的requestAlwaysAuthorization
并将其降级为requestWhenInUseAuthorization
- 这意味着作为开发人员,我们现在必须在Info.plist
I found that they have added a new key NSLocationAlwaysAndWhenInUseUsageDescription
我发现他们添加了一个新密钥 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.
*/
However, upon using this new key - the location service still didn't work, upon further searching I found this gem mixed in with all the extra debugging information:
但是,在使用这个新密钥时 - 位置服务仍然不起作用,进一步搜索后,我发现这个 gem 与所有额外的调试信息混合在一起:
This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription keys with string values explaining to the user how the app uses this data
此应用程序试图在没有使用说明的情况下访问隐私敏感数据。应用程序的 Info.plist 必须包含 NSLocationAlwaysAndWhenInUseUsageDescription 和 NSLocationWhenInUseUsageDescription 键,并带有字符串值向用户解释应用程序如何使用这些数据
Which directly contradicts the the comment that I found in the updated CLLocationManager.h
file. So I've created a radar.
这直接与我在更新CLLocationManager.h
文件中找到的评论相矛盾。所以我创建了一个雷达。
Good news, if you follow the advice of the debugging console, IE. add both the new key NSLocationAlwaysAndWhenInUseUsageDescription
and one of the old keys NSLocationWhenInUseUsageDescription
, locations services will start to work again.
好消息,如果您遵循调试控制台 IE 的建议。添加新密钥NSLocationAlwaysAndWhenInUseUsageDescription
和旧密钥之一NSLocationWhenInUseUsageDescription
,位置服务将再次开始工作。
回答by Heider Sati
Just to add the steps on fixing this:
只是添加解决此问题的步骤:
2 ways to do it:
2种方法:
A) The easy way: Select your Info.plist file, add the properties, note that they start with PRIVCY instead of LOCATION... therefore, the exact names of these variables starts with "Privacy - Location ... " etc, add each here, and describe how the user would be seeing this on the warning.
A) 简单的方法:选择您的 Info.plist 文件,添加属性,注意它们以 PRIVCY 而不是 LOCATION 开头...因此,这些变量的确切名称以“Privacy - Location ...”等开头,添加每个在这里,并描述用户将如何在警告中看到这一点。
B) The hard / interesting / programatic way (I like this way more):
B)硬/有趣/程序化的方式(我更喜欢这种方式):
Right click on your Info.plist for your app, and then select "View source code", you should see it all in XML,
右键单击您的应用程序的 Info.plist,然后选择“查看源代码”,您应该会在 XML 中看到所有内容,
Follow the other ...... format, and add these properties as follows:
按照其他......格式,并添加这些属性如下:
<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>
Save, and then right-click on the info.plist file, and then select Property list, this should view the file back into the default view.
保存,然后右键单击info.plist 文件,然后选择Property list,这样应该会将文件查看回默认视图。
EDIT:
编辑:
Another member asked for code, here it is:
另一位成员要求提供代码,这里是:
1) On your .H file, add:
1) 在您的 .H 文件中,添加:
@property (strong, nonatomic) CLLocationManager *LocationManager;
2) On your .M file add under ViewDidAppear() function:
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];
This what works fine for me, hopefully the code would work for you too.
这对我来说很好用,希望代码也适合你。
Regards
问候
Heider
海德
回答by iDoc
working under iOS11 i discovered, that Info.plist needs al least NSLocationAlwaysAndWhenInUseUsageDescription in Info.plist:
在 iOS11 下工作我发现,Info.plist 在 Info.plist 中至少需要 NSLocationAlwaysAndWhenInUseUsageDescription:
Strange enough when your app is multilingualthe localized versions of your strings need all three keysmentioned in this post else requestAlwaysAuthorization()
and locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
will fail silently.
奇怪的是,当您的应用程序是多语言时,您的字符串的本地化版本需要本文中提到的所有三个键,requestAlwaysAuthorization()
并且locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
会静默失败。
Shot showing german translation as example:
以德语翻译为例的镜头:
Hope this saves you time when stumbling upon.
希望这可以节省您偶然发现的时间。
回答by Keshav Gera
Working in Swift 4.0.3
在 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>
回答by mhit0
Follow these steps:
按着这些次序:
I ran into the same issue with an app that needed "Always Authorization", and resolved it by following these steps:
我在需要“始终授权”的应用程序中遇到了同样的问题,并按照以下步骤解决了这个问题:
1.AddNSLocationWhenInUseUsageDescription
key to Info.plist
1.添加NSLocationWhenInUseUsageDescription
密钥Info.plist
2.AddNSLocationAlwaysAndWhenInUseUsageDescription
to Info.plist
2.添加NSLocationAlwaysAndWhenInUseUsageDescription
到Info.plist
3.AddNSLocationAlwaysUsageDescription
to Info.plist
(to support < iOS 11)
3.添加NSLocationAlwaysUsageDescription
到Info.plist
(支持<iOS 11)
4.CallrequestWhenInUseAuthorization()
BEFORErequestAlwaysAuthorization(
)
4.之前打电话)requestWhenInUseAuthorization()
requestAlwaysAuthorization(
You cannot execute requestAlwaysAuthorization() before requestWhenInUseAuthorization(). You must escalate to that permission level. Once I made these changes, location updates started working properly again.
您不能在 requestWhenInUseAuthorization() 之前执行 requestAlwaysAuthorization()。您必须升级到该权限级别。进行这些更改后,位置更新再次开始正常工作。
More details can be found here:
更多详情可在这找到:
回答by Abhishek Bedi
Better safe than sorry ..In iOS 11 : Add the below and you are good.
安全总比抱歉好..在 iOS 11 中:添加以下内容,您就可以了。
<key>NSLocationWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Description</string>
回答by swiftBoy
Tested on iOS 12.2with Swift 5
使用Swift 5在iOS 12.2上测试
Step 1. you must add following privacy permissions in plist file
步骤 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>
Step 2. make sure you have following swift code to get current locations
第 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)
}
}
回答by Santosh Sahoo
Swift : 3i have faced the same issue. i was totally screwed up finding the solution. here is how i fixed the issue.
斯威夫特:3我遇到了同样的问题。我完全搞砸了找到解决方案。这是我解决问题的方法。
step-1 :Project file > Capabilities > background modes > select Location Update
步骤 1:项目文件 > 功能 > 背景模式 > 选择位置更新
step-2 :Add NSLocationWhenInUseUsageDescription , NSLocationAlwaysAndWhenInUseUsageDescription keys to Info.plist
第 2 步:将 NSLocationWhenInUseUsageDescription 、 NSLocationAlwaysAndWhenInUseUsageDescription 键添加到Info.plist
step-3 :
第 3 步:
manager.pausesLocationUpdatesAutomatically = false
manager.allowsBackgroundLocationUpdates = true