iOS 10中位置权限对话框频繁弹出

3

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>

你在哪里调用了startLocationManager? - Anbu.Karthik
@Anbu.Karthik 在 appdelegate 中.. didlaunch - Bhavin Bhadani
从那里移除,并添加到某个类中,然后尝试一次。 - Anbu.Karthik
抱歉,这不是一个演示项目...它在iOS 9上工作正常,但在10上有时会出现很多提示,应用程序无法处理。 - Bhavin Bhadani
在调用 requestWhenInUseAuthorization 之前,请检查授权状态。 - Sachin Vas
显示剩余5条评论
2个回答

1

无论使用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;
    }
}

好的,让我试试这个。 - Bhavin Bhadani

0

也许您可以尝试以下检查,看看是否有帮助:

不要每次都调用requestWhenInUseAuthorization

检查locationServicesEnabledauthorizationStatus,只有在authorizationStatuskCLAuthorizationStatusDeniedlocationServicesEnabled返回false时才调用requestWhenInUseAuthorization

例如:

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

        [self.locationManager requestWhenInUseAuthorization];

    }
}

希望能有所帮助 :)

1
好的,我会尝试这个方法并检查它是否有帮助。谢谢。 - Bhavin Bhadani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接