【问题标题】:Location permission dialog prompts lots of time in iOS 10iOS 10 中位置权限对话框提示大量时间
【发布时间】:2016-09-26 05:24:00
【问题描述】:

在 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 in appdelegate .. didlaunch
  • 你能附上你的项目吗
  • 从那里删除并添加一些类并尝试一次
  • 对不起,它不是一个演示项目......它在 iOS 9 中的工作,但在 10 中有时会提示这么多,无法由应用程序处理

标签: ios objective-c ios10


【解决方案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;
    }
}

【讨论】:

  • 好吧,让我试试这个
【解决方案2】:

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

不要每次都打电话给requestWhenInUseAuthorization

检查locationServicesEnabledauthorizationStatus,仅当authorizationStatuskCLAuthorizationStatusDeniedlocationServicesEnabled 返回false 时才调用requestWhenInUseAuthorization

喜欢,

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

        [self.locationManager requestWhenInUseAuthorization];

    }
}

希望它会有所帮助:)

【讨论】:

  • 你确定..我也会试试这个,看看它是否有帮助..谢谢
猜你喜欢
  • 1970-01-01
  • 2017-02-20
  • 2022-09-26
  • 1970-01-01
  • 2017-05-06
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
相关资源
最近更新 更多