[英]Location Services not working in iOS 11
我剛用iOS 11 SDK重建了我的應用程序,試圖刪除現在總是出現的blue banner
。 我想 - “太棒了,有用”,卻發現定位服務現在根本不起作用。
用於iOS 10的應用程序 - 有沒有人聽到過什么?
蘋果似乎又添加了另一項隱私功能。 用戶現在可以覆蓋我們的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
,位置服務將再次開始工作。
只是添加修復此步驟的步驟:
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>
單擊保存,然后右鍵單擊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];
這對我來說很好,希望代碼也適合你。
問候
海德
在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>
我遇到了一個需要“始終授權”的應用程序遇到了同樣的問題,並通過以下步驟解決了這個問題:
1. 將 NSLocationWhenInUseUsageDescription
鍵添加到Info.plist
2. 將 NSLocationAlwaysAndWhenInUseUsageDescription
添加到Info.plist
3. 將 NSLocationAlwaysUsageDescription
添加到Info.plist
(以支持<iOS 11)
4. 在 requestAlwaysAuthorization(
) 之前 調用 requestWhenInUseAuthorization()
requestAlwaysAuthorization(
)
您無法在requestWhenInUseAuthorization()之前執行requestAlwaysAuthorization()。 您必須升級到該權限級別。 完成這些更改后,位置更新再次開始正常工作。
更多詳情可在這找到:
比抱歉更安全..在iOS 11中:添加以下內容,你很好。
<key>NSLocationWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Description</string>
斯威夫特:3我遇到了同樣的問題。 我完全搞砸了找到解決方案。 這是我如何解決這個問題。
步驟1:項目文件>功能>后台模式>選擇位置更新
步驟2:將NSLocationWhenInUseUsageDescription,NSLocationAlwaysAndWhenInUseUsageDescription鍵添加到Info.plist
第3步:
manager.pausesLocationUpdatesAutomatically = false
manager.allowsBackgroundLocationUpdates = true
使用Swift 5在iOS 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>
第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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.