[英]Current location permission dialog disappears too quickly
我的應用程序獲取用戶的位置,獲取坐標,並提供與他們的目的地或原點之間的距離。 所有這些可能的目的地都顯示在表格視圖中,因此我在填充表格的同時獲取用戶坐標。 唯一的問題是,詢問用戶位置的警報視圖出現然后消失得如此之快,以至於無法點擊它!
有沒有辦法在應用程序首次加載時手動顯示此警報? 我嘗試在應用加載時獲取用戶的位置以嘗試強制顯示警報,但這沒有用。
雖然難以追蹤,但解決方案非常簡單。
通過多次反復試驗,我發現當您第一次嘗試訪問應用程序中的任何位置服務時會彈出位置訪問對話框,如果CLLocationManager
對象被釋放,該對話框會自行消失(無需任何用戶交互)在用戶響應對話框之前。
我在viewDidLoad
方法中創建了一個CLLocationManager
實例。 由於這是該方法的本地實例,該實例在該方法執行完成后由 ARC 釋放。 實例一釋放,對話框就消失了。 解決方案相當簡單。 將CLLocationManager
實例從方法級變量更改為類級實例變量。 現在CLLocationManager
實例僅在類卸載后才釋放。
我也遇到過類似的情況。 調試后發現
let locationManager = CLLocationManager()
在方法范圍內調用,但應該全局調用。
為什么?
簡而言之,locationManager 在方法返回后已被釋放。 但它不應該被釋放,直到用戶給予或拒絕許可
相同的症狀,不同的原因:不要startUpdatingLocation
多次調用startUpdatingLocation
。
我不小心構造了一些東西,以至於代碼無意中startUpdatingLocation
兩次調用startUpdatingLocation
,這顯然很糟糕。 這也可能與隊列的選擇有關,因為我正在等待等待網絡請求的結果開始更新,但我不需要做任何 GCD 魔法來修復它......只需要確保我沒有重復開始。
希望有人能從我的痛苦中受益。 :)
我知道這是一個很晚的答復。 但它可能會幫助某人。 我也遇到了同樣的問題,花了一個小時來確定問題。 起初我的代碼是這樣的。
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
CLLocation *location = locationManager.location;
//my stuff with the location
[locationManager release];
現在位置警報很快消失了。 當我取消注釋最后一行時,它工作正常。
// [locationManager release];
我陷入了同樣的問題(至少是症狀)。 就我而言,問題出在- (void)applicationWillResignActive:(UIApplication *)application;
方法,我在其中發布了我的CLLocationManager
實例,作為准備背景轉換的一部分。 當我刪除它並只將它留在- (void)applicationDidEnterBackground:(UIApplication *)application;
問題消失了。
棘手的部分是核心位置警報確實會在您的應用程序仍處於前台時暫停它。
希望它會幫助你,我花了很多時間才找到那個混蛋:)
我也遇到了這個問題,但在我的案例中的解決方案與公認的答案完全不同。
在我的應用程序中,我從applicationWillResignActive
調用stopUpdatingLocation
。 這是一個問題,因為在出現權限對話框時會調用applicationWillResignActive
。 這是造成stopUpdatingLocation
后立即startUpdatingLocation
,這就是為什么對話框將立即消失。
解決方案只是從applicationDidEnterBackground
調用stopUpdatingLocation
。
我在使用 iOS 模擬器時發生了這種情況。 我確定它正在發生是因為我的運行方案正在模擬一個位置。 我認為這與在啟動時調用locationManager.startUpdatingLocation()
具有相同的效果,因此它正在關閉對話框。
取消選中“編輯方案”對話框中的“允許位置模擬”復選框修復了該問題。 一旦它按您的意願工作並設置了權限,您就可以重新啟用位置模擬,此后模擬器將正常工作。
確保在您的.plist
文件中添加了隱私行( always和whenInUse )並將CoreLocation
Framework 添加到您的項目中
當我更改時,位置權限對話框會正確顯示:
locationManager.requestAlwaysAuthorization()
和:
locationManager.requestWhenInUseAuthorization()
PS .:我已經嘗試了所有建議,但都失敗了(請求授權viewDidLoad
, var
而不是let
locationManager,請求后不要啟動startUpdatingLocation()
我認為這是一個錯誤,我希望他們能盡快解決它盡可能..
我將locationManager
作為實例變量,但在 Swift 5、Xcode 11 中仍然沒有幫助(見下文):
class MapViewController: UIViewController {
var locationManager: CLLocationManager {
let locationManager = CLLocationManager()
locationManager.desiredAccuracy = .greatestFiniteMagnitude
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
return locationManager
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.startUpdatingLocation()
}
}
但是,使locationManager
var lazy 解決了這個問題:
class MapViewController: UIViewController {
lazy var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.desiredAccuracy = .greatestFiniteMagnitude
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
return locationManager
}()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.startUpdatingLocation()
}
}
SWIFT 4 @Zoli 解決方案將如下所示:
class WhateverViewController: UIViewController {
let locationManager = CLLocationManager() // here is the point of the @Zoli answer
// some code
override func viewDidLoad() {
super.viewDidLoad()
// some other code
locationManager.requestWhenInUseAuthorization()
// some other code
}
}
您最常將 locationManager 變量定義為全局對象。
@interface ViewController : UIViewController
{
CLLocationManager *locationManager;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
}
我遇到了和你一樣的情況。
#import @interface ViewController () @end @implementation ViewController @synthesize locManager; // after - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //MyLocationService *locManager = [[BSNLocationService alloc]init:nil]; // before. the loc. delegate did not work because the instance became invalid after this method. self->locManager= [[MyLocationService alloc]init:nil]; // after locManager.startService; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.