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
@implementation CLLocationManager (Extend)
+ (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