Assertion failure in -[CLLocationManager setAllowsBackgroundLocationUpdates:], /BuildRoot/……

问题:

 *** Assertion failure in -[CLLocationManager setAllowsBackgroundLocationUpdates:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework/CoreLocation-2245.8.22/Framework/CoreLocation/CLLocationManager.m:652
2019-03-06 21:28:30.367097+0800 QuickinClient[285:5927] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: !stayUp || CLClientIsBackgroundable(internal->fClient)'

问题出现原因:项目中需要使用地图的单次定位,但使用单次定位需要开启项目的Capabilities - Background Modes - Location updates后台定位模式。开启此选项App审核会被拒,需要说明后台使用定位的原因;若不开启,那么百度地图获取单次定位的

-(void)initLocation
{
    
    _locationManager = [[BMKLocationManager alloc] init];
    //设置delegate
    _locationManager.delegate = self;
    //设置返回位置的坐标系类型
    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    //设置距离过滤参数
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //设置预期精度参数
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置应用位置类型
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //设置是否自动停止位置更新
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    //设置是否允许后台定位
    _locationManager.allowsBackgroundLocationUpdates = YES;
    //设置位置获取超时时间
    _locationManager.locationTimeout = 60;
    //设置获取地址信息超时时间
    _locationManager.reGeocodeTimeout = 10;
    self.completionBlock = ^(BMKLocation *location, BMKLocationNetworkState state, NSError *error)
    {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
        }
        if (location) {//得到定位信息,添加annotation
            
            if (location.location) {
                NSLog(@"LOC = %@",location.location);
            }
            if (location.rgcData) {
                NSLog(@"rgc = %@",[location.rgcData description]);
                [now_address setTitle:location.rgcData.city forState:UIControlStateNormal];
                now_address
                .sd_layout
                .widthIs(20+[UILabel getWidthWithTitle:location.rgcData.city font:[UIFont systemFontOfSize:11]]);
                AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
                [appDelegate.nowlocation setObject:location.rgcData.city forKey:@"city"];
            }
        }
        NSLog(@"netstate = %d",state);
    };
    
    
    [_locationManager requestLocationWithReGeocode:_isNeedAddr withNetworkState:_isNeedHotSpot completionBlock:self.completionBlock];
}

地方不会走block回调,还有可能报错,解决方法如下:
1.在项目中创建CLLocationManager的Category,并添加如下代码:


#import "CLLocationManager+ZHExtension.h"
#import 
@implementation CLLocationManager (ZHExtension)
+ (void)load {
    if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {
        method_exchangeImplementations(class_getInstanceMethod(self.class, NSSelectorFromString(@"setAllowsBackgroundLocationUpdates:")), class_getInstanceMethod(self.class, @selector(swizzledSetAllowsBackgroundLocationUpdates:)));
    }
}

- (void)swizzledSetAllowsBackgroundLocationUpdates:(BOOL)allow {
    if (allow) {
        NSArray *backgroundModes = [[NSBundle mainBundle].infoDictionary objectForKey:@"UIBackgroundModes"];
        if( backgroundModes && [backgroundModes containsObject:@"location"]) {
            [self swizzledSetAllowsBackgroundLocationUpdates:allow];
        } else {
            NSLog(@"APP想设置后台定位,但APP的info.plist里并没有申请后台定位");
        }
    } else {
        [self swizzledSetAllowsBackgroundLocationUpdates:allow];
    }
}

@end

2.将CLLocationManager的Category 放入全局的.pch文件中即可。

你可能感兴趣的:(iOS踩过的坑)