Ios startMonitoringSignificantLocationChanges不调用didUpdateLocations:当应用程序关闭时
Im使用IBeacon构建一个简单的ios应用程序,Im使用startMonitoringForRegion检测信标 好的 我还想在应用程序处于后台或关闭状态时检查用户位置,为此,我使用startMonitoringSignificantLocationChanges。在后台模式下工作正常,但当应用程序关闭时,不会触发didUpdateLocations:回调 我的代码(来自AppDelegate.m)如下:Ios startMonitoringSignificantLocationChanges不调用didUpdateLocations:当应用程序关闭时,ios,objective-c,location,Ios,Objective C,Location,Im使用IBeacon构建一个简单的ios应用程序,Im使用startMonitoringForRegion检测信标 好的 我还想在应用程序处于后台或关闭状态时检查用户位置,为此,我使用startMonitoringSignificantLocationChanges。在后台模式下工作正常,但当应用程序关闭时,不会触发didUpdateLocations:回调 我的代码(来自AppDelegate.m)如下: - (BOOL)application:(UIApplication *)ap
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
[self.locationManager startMonitoringSignificantLocationChanges];
NSString *message = @"UIApplicationLaunchOptionsLocationKey";
[self sendLocalNotificationWithMessage:message];
}
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
NSString *idN = [localNotif.userInfo objectForKey:@"id_notif"];
[self sendToDetail:idN];
}
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
NSLog(@"RESPONDS!!!!!");
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]];
}
[self resetBadge];
// Override point for customization after application launch.
NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6A"];
NSString *regionIdentifier = @"iBeacons region 1";
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID: beaconUUID identifier: regionIdentifier ];
beaconRegion.notifyEntryStateOnDisplay = YES;
self.locationManager = [[CLLocationManager alloc]init];
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[self.locationManager requestAlwaysAuthorization];
}
self.locationManager.delegate = self;
//self.locationManager.pausesLocationUpdatesAutomatically = NO;
[self.locationManager startMonitoringForRegion:beaconRegion];
return YES;
}
- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
[manager startRangingBeaconsInRegion:(CLBeaconRegion*) region];
NSLog(@"didEnterRegion");
}
- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
[manager stopRangingBeaconsInRegion:(CLBeaconRegion*) region];
NSLog(@"didExitRegion");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"startMonitoringSignificantLocationChanges");
[self.locationManager startMonitoringSignificantLocationChanges];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"stopMonitoringSignificantLocationChanges");
[self.locationManager stopMonitoringSignificantLocationChanges];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSString *message = @"didUpdateLocations";
[self sendLocalNotificationWithMessage:message];
CLLocation *currentLocation = locations[0];
if (currentLocation != nil) {
NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSString *message = [NSString stringWithFormat:@"longitud: %@ latitud: %@", longitude,latitude ];
[self sendLocalNotificationWithMessage:message];
}
}
有什么想法吗
谢谢您等待此回调多长时间?我了解到startMonitoringSignificantLocationChanges仅在检测到设备相关基站的变化时才会发送新事件,从而减少更新频率并显著降低功耗。所以我在上班的时候检查它,大约5公里。“它不应该期望通知的频率超过每五分钟一次”-从docsThanks,但不是时间问题,我等了几个小时,没有发送通知。我添加了一些代码,我在didUpdateLocations:callback中触发了一个通知。当应用程序关闭时,didFinishLaunchingWithOptions:中的通知正在启动,但didUpdateLocations中的通知:否。