简体   繁体   English

位置权限对话框在iOS 10中提示了大量时间

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

in iOS 10, sometimes when install the app, location permission prompts opens lots of time and hangs all app and not able to move further. 在iOS 10中,有时在安装应用程序时,位置权限提示会打开大量时间并挂起所有应用程序而无法进一步移动。

here is my code that works before 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 ){

        }
    }
}

In my plist file, 在我的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>

Regardless of iOS 10, you should start your location updating only if the permission was granted, you should also check if the permission is already granted before requesting permissions: 无论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;
    }
}

May be you can try below checks and see if it helps: 也许你可以尝试下面的检查,看看它是否有帮助:

Do not call requestWhenInUseAuthorization every time. 每次都不要调用requestWhenInUseAuthorization

check for both locationServicesEnabled and authorizationStatus , call for requestWhenInUseAuthorization only if authorizationStatus is kCLAuthorizationStatusDenied and locationServicesEnabled return false . 检查locationServicesEnabledauthorizationStatus ,仅当authorizationStatuskCLAuthorizationStatusDenied并且locationServicesEnabled返回false时才调用requestWhenInUseAuthorization

Like, 喜欢,

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

        [self.locationManager requestWhenInUseAuthorization];

    }
}

Hope it will help:) 希望它会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM