startMonitoringSignificantLocationChanges不调用didUpdateLocations:应用程序关闭时

时间:2015-02-05 09:24:12

标签: ios objective-c location

我正在使用IBeacon构建一个简单的ios应用程序,我使用startMonitoringForRegion来检测信标。

多数工作正常。

我还想在应用程序为后台或关闭时检查用户位置,为此目的我使用startMonitoringSignificantLocationChanges。在后台模式下工作正常,但是当应用程序关闭时,不会触发didUpdateLocations:callback。

我的代码(来自AppDelegate.m)如下:

    - (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];

        }
    }

有什么想法吗?

由于

0 个答案:

没有答案