簡體   English   中英

即使使用Info.plist中的NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription,iOS locationmanager也不要求用戶許可

[英]iOS locationmanager doesn't ask user permission even with NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription in Info.plist

提交我的應用程序后,我需要使用CoreLocation(#import)獲取用戶位置,但是它永遠不會問我權限警報。

ViewController.h

#import "RootViewController.h"
#import < UIKit/UIKit.h >
#import < CoreLocation/CoreLocation.h >

@interface AroundMeViewController : RootViewController <CLLocationManagerDelegate>

@property(nonatomic, strong) CLLocationManager *locationManager;

...

@end

__________

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

NSLog(@"LOG: %d", [CLLocationManager locationServicesEnabled]);

self.locationManager = [[CLLocationManager alloc] init];
// Set a delegate to receive location callbacks
self.locationManager.delegate = self;

// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [self.locationManager requestAlwaysAuthorization];
}

....
// Start the location manager
[self.locationManager startUpdatingLocation];

NSLog(@"LOG: %@", self.locationManager);

.....

}

我確實嘗試了很多方法來解決此問題(不同的設備,允許通過隱私設置手動設置),但我不知道我的錯誤在哪里。

根據NSHipster的說法 ,必須明確要求使用位置服務,並且是異步授予的,因此我們不能像以前那樣立即啟動位置更新。 我們需要實施

    locationManager:didChangeAuthorizationStatus

委托方法並在那里開始更新位置。 每當身份驗證狀態基於用戶輸入而更改時,都會調用此方法。

似乎在設置locationManager時也會調用此委托方法,因為我的觸發器是在初始屏幕仍顯示時觸發的(我懷疑是在設置locationManager委托時),然后在調用triggerLocationServices()時再次觸發了。 這實際上可能是“沉默”您的請求。

我將方法更改為以下內容:

    func triggerLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            if self.locationManager.respondsToSelector("requestAlwaysAuthorization") {
                // request authorization to use location
            } else {
                // start updating location
            }
        }
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus){
        if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
            // start updating location
        } 
        else {
            triggerLocationServices()
        }
    }

並刪除了對triggerLocationServices()的所有其他調用。 這與正確的info.plist鍵一起為我解決了此問題。

很抱歉,您可以將Swift代碼翻譯成Obj-C。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM