![](/img/trans.png)
[英]CLLocationManager requestAlwaysAuthorization not showing alert second time
[英]requestAlwaysAuthorization not showing permission alert
我一直在嘗試使用一些花哨的 iBeacons,但沒有成功,kCLAuthorizationStatusNotDetermined。 根據其他問題,需要將這些鍵添加到 info.plist (有些問題說一個,其他說兩個)。 根據 iBeacons 的一篇文章,我需要 Always 選項。
<key>NSLocationWhenInUseUsageDescription</key>
<string>Nothing to say</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Permiso para acceder siempre</string>
在viewDidAppear:
self.locManager = [[CLLocationManager alloc]init];
self.locManager.delegate = self;
[self.locManager requestAlwaysAuthorization];
NSUUID* region1UUID = [[NSUUID alloc]initWithUUIDString:@""]; //ibeacon real UUID between "". Checked it's not nil.
self.beaconRegion = [[CLBeaconRegion alloc]
initWithProximityUUID:proximityUUID
identifier:@"myCoolString"];
self.beaconRegion.notifyEntryStateOnDisplay = YES;
self.beaconRegion.notifyOnEntry = YES;
self.beaconRegion.notifyOnExit = NO;
[self.locManager startMonitoringForRegion:self.beaconRegion];
[self.locManager startRangingBeaconsInRegion:self.beaconRegion];
圖標沒有出現在設置/隱私/位置,直到它被執行最后兩個方法之一。 永遠不會出現批准權限的警報視圖。 如果我在“位置設置”中執行手動更改並檢查它會更改狀態,但稍后“設置中的位置”將刪除我的應用程序的“始終”狀態,並將其再次留空。 后來我檢查沒有運氣
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
任何想法丟失或錯誤? 謝謝
對於 iOS 11 開發人員,您應該看看這篇文章: 定位服務在 iOS 11 中不起作用。
TL;DR:您需要 Info.plist 中的所有三個位置鍵:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>...</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>...</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>...</string>
在多語言應用程序的情況下,連同InfoPlist.strings
翻譯。
有完全相同的問題。 原來,在我的情況下, NSLocationAlwaysUsageDescription
在我被要求InfoPlist.strings
本地化的文件。 在Info.plist
中擁有NSLocationAlwaysUsageDescription
是不夠的......
我注意到,如果您的 CLLocationManager 實例在警報顯示之前被銷毀,您將永遠不會看到警報。 就我而言,我在 AppDelegate 中創建了位置管理器的局部變量以請求許可。
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];
}
將局部變量更改為實例變量使警報顯示:
@interface AppDelegate () {
CLLocationManager *_locationManager;
}
@end
_locationManager = [[CLLocationManager alloc] init];
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_locationManager requestAlwaysAuthorization];
}
我最近在嘗試使用 IOS 11 時遇到了類似的問題
locationManager.requestAlwaysAuthorization()
我的解決方案是在 plist.info 中添加所有 4 個權限以獲取警報:
只需將此行添加到您的 .plist 文件中
<key>NSLocationAlwaysUsageDescription</key>
<string>Optional message</string>
嘗試開始更新位置(對我有幫助)
[self.locationManager startUpdatingLocation];
對於 ios 11 重要的是添加下面提到的新權限
NSLocationAlwaysAndWhenInUseUsageDescription
連同舊的許可
NSLocationAlwaysUsageDescription,NSLocationWhenInUseUsageDescription
修復 requestAlwaysAuthorization 未顯示權限警報的問題。
發現,在論壇上記錄並測試它是一個與 Objective-C iOS 8 相關的錯誤。 用 Swift 編寫的相同代碼也能正常工作。 工作快速代碼,委托是調用。
將核心位置框架添加到項目設置/目標/功能/后台模式設置“位置更新”和“使用藍牙 LE 附件”在 Info.plist NSLocationAlwaysUsageDescription 添加密鑰
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locManager: CLLocationManager?
override func viewDidLoad() {
super.viewDidLoad()
self.locManager = CLLocationManager();
self.locManager!.delegate = self;
if (!CLLocationManager.locationServicesEnabled()) {
println("Location services are not enabled");
}
self.locManager!.requestAlwaysAuthorization();
self.locManager!.pausesLocationUpdatesAutomatically = false;
let uuidString = "" // My ibeacon string there
let beaconIdentifier = "myCompany"
let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)
let beaconRegion:CLBeaconRegion = CLBeaconRegion(proximityUUID: beaconUUID,
identifier: beaconIdentifier)
self.locManager!.startMonitoringForRegion(beaconRegion)
self.locManager!.startRangingBeaconsInRegion(beaconRegion)
self.locManager!.startUpdatingLocation()
}
對話框出現 OK
確保將密鑰添加到正確的 Info.plist 文件中。 不要忘記有一個用於您的應用程序,一個用於 AppTest。
Swift 3.X 最新代碼輕松使用
import CoreLocation
public var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let altitudeG = locations.last?.altitude
let longitudeG = locations.last?.coordinate.longitude
let latitudeG = locations.last?.coordinate.latitude
print("\(altitudeG) \(longitudeG) \(latitudeG)")
}
硬件相關系統問題?
iOS 13.1.3、iPhone 7(型號 ID“iPhone9,1”)
似乎系統設置-> 隱私-> 位置以某種方式緩存權限,即使在刪除應用程序后(至少,在同一 iOS 會話期間和指定配置中)。
如果按下“允許一次”並且用戶刪除並重新安裝並再次運行應用程序,didChangeAuthorizationStatus(_:) 會發送初始狀態 .denied。 (因此,根據 CLLocationManager 邏輯,它不會在 requestAlwaysAuthorization() 之后顯示提示警報)。 在這種情況下,這種 .denied 狀態是邪惡的根源。
如果用戶在初始 didChangeAuthorizationStatus( :) 和 requestAlwaysAuthorization()之間折疊應用程序,這會導致 didChangeAuthorizationStatus( :) 的下一次更新狀態不是 .denied,並且將顯示提示警報。
iOS 13.1.3、iPhone 7(型號 ID“iPhone9,3”) - 問題不會重現。 (收到的初始狀態是 .notDetermined)
iOS 13.1.2 iPhone 8 - 一切正常,同上
我復制了這個教程...
http://willd.me/posts/getting-started-with-ibeacon-a-swift-tutorial
它沒有解決這個問題,雖然修復很簡單,但不要聲明
讓 locationManager = CLLocationManager()
但是將它作為變量移到類中
var locationManager = CLLocationManager()
它有效!!
當您的應用程序的位置權限未設置時,它會顯示一個提示,一旦為您的應用程序設置了“使用中”位置權限,后續調用就不會顯示(我認為沒有任何 API 反饋表明電話被吞了)。
來自蘋果文檔:
討論 當當前授權狀態為 notDetermined 時,該方法異步運行,並提示用戶授予應用使用位置服務的權限。 用戶提示包含來自應用程序的 Info.plist 文件中 NSLocationAlwaysUsageDescription 鍵的文本,並且在調用此方法時需要該鍵的存在。 確定狀態后,位置管理器將結果傳遞給委托的 locationManager( :didChangeAuthorization:) 方法。 如果當前授權狀態不是 notDetermined,則此方法不執行任何操作,並且不會調用 locationManager( :didChangeAuthorization:) 方法,只有一個例外。 如果您的應用從未請求過始終授權,並且其當前授權狀態為 authorizedWhenInUse ,您可以調用此方法一次嘗試將您的應用的授權狀態更改為 authorizedAlways 。
請檢查此參考鏈接
它描述了有關 iOS 11 的所有更改。您的問題似乎也是它所描述的案例之一。 它可能會讓您了解需要在代碼中進行哪些更改才能更正問題。
對於我在 iOS 13 上,我不得不重置模擬器以再次觸發權限提示。
添加:確保您已將數組 UIBackgroundModes 添加到您的 .plist 並插入“位置”值。
Apple 不會告訴你它丟失了,而是給你 WhenInUse。
所以總結一下:
最近,IOS 13,*用戶重新安裝應用程序后保存結果請求權限
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.