簡體   English   中英

位置權限對話框在iOS 10中提示了大量時間

[英]Location permission dialog prompts lots of time in iOS 10

在iOS 10中,有時在安裝應用程序時,位置權限提示會打開大量時間並掛起所有應用程序而無法進一步移動。

這是我的代碼在iOS 10之前有效

-(void)startLocationManager{  
    self.locationManager=[[CLLocationManager alloc]init];
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationManager.delegate=self;
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

    [self.locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    if (self.myCurrentLocation==nil) {
        self.myCurrentLocation=[locations lastObject];
        [[WALocationManager WALocationSharedInstance] checkLatestLocation];
    }
    else{
        if (self.myCurrentLocation.horizontalAccuracy < 0){
            return;
        }
        self.myCurrentLocation=[locations lastObject];
        if([[WALocationManager WALocationSharedInstance] currentLocation]!=self.myCurrentLocation ){

        }
    }
}

在我的plist文件中,

<key>NSLocationAlwaysUsageDescription</key>
<string>This app will use your location to get most nearyby activity around you.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app will use your location.</string>

無論iOS 10如何,只有在授予權限的情況下才應啟動位置更新,還應在請求權限之前檢查是否已授予權限:

-(void)startLocationManager{
    self.locationManager=[[CLLocationManager alloc]init];
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationManager.delegate=self;
    // Check for current permissions
    [self checkLocationAuth:[CLLocationManager authorizationStatus]];
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    [self checkLocationAuth:status];
}


-(void)checkLocationAuth:(CLAuthorizationStatus)status{
    switch (status) {
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways:
            [self.locationManager startUpdatingLocation];
            break;
            // did not ask for permission, ask now
        case kCLAuthorizationStatusNotDetermined:
            if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            } else { // iOS < 8? implicitly request permission
                [self.locationManager startUpdatingLocation];
            }
            break;
        // Also need to handle failures, etc
        default:
            break;
    }
}

也許你可以嘗試下面的檢查,看看它是否有幫助:

每次都不要調用requestWhenInUseAuthorization

檢查locationServicesEnabledauthorizationStatus ,僅當authorizationStatuskCLAuthorizationStatusDenied並且locationServicesEnabled返回false時才調用requestWhenInUseAuthorization

喜歡,

if(![CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
   if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

        [self.locationManager requestWhenInUseAuthorization];

    }
}

希望它會有所幫助:)

暫無
暫無

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

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