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

来源:互联网 发布:教育软件开发公司 编辑:程序博客网 时间:2024/05/22 13:23

iOS项目集成百度地图的时候必须设置后台模式,而AppStore上线时,项目中没有用到后台定位,导致2.16被拒,苹果要求你勾选掉Background Modes 中的Location Updates

但是当你勾选掉这个后台模式后,程序crash。crash原因如下:

*** Assertion failure in -[CLLocationManager setAllowsBackgroundLocationUpdates:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-1861.3.25.49/Framework/CoreLocation/CLLocationManager.m:609

断言失败:提示需要设置后台模式

冲突问题对比:

1.程序不勾选后台模式:

*** Assertion failure in -[CLLocationManager setAllowsBackgroundLocationUpdates:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-1861.3.25.49/Framework/CoreLocation/CLLocationManager.m:609

2.程序勾选上后台模式,但是app中没有用到后台定位:

苹果被拒反馈:2.16 - Multitasking Apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion, local notifications, etc.

解决方案:

在项目中创建CLLationManager的类目,并导入到.pch文件中作为全局的,使用runtime方式来监听,代码如下:


#import "CLLocationManager+Extend.h"

#import <objc/runtime.h>


@implementation CLLocationManager (Extend)


+ (void)load {

    

    if ([UIDevicecurrentDevice].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  = [[NSBundlemainBundle].infoDictionaryobjectForKey:@"UIBackgroundModes"];

        

        if( backgroundModes && [backgroundModescontainsObject:@"location"]) {

            [selfswizzledSetAllowsBackgroundLocationUpdates:allow];

        }else{

            NSLog(@"APP想设置后台定位,但APPinfo.plist里并没有申请后台定位");

        }

    }else{

        [selfswizzledSetAllowsBackgroundLocationUpdates:allow];

    }

}


@end



0 0