Start tracking location updates using Apple Watch's locationManager while the iPhone app is in the background

I am working on a very simple GPS application where users can follow their driving. Typically this is what a custom driver does:

  • go to his / her car.
  • run the application
  • press the button that says "I'm going"
  • disk
  • press the button "I have arrived" when you have arrived at your destination

I want the Apple Watch companion app to find these two buttons. Thus, there are two scenarios:

  • A / When the app is in the foreground , no problem.

  • B / When the app is in the background or not running , although I am not getting any gps updates. Nothing happened. Nada.

Here is the code.

When the user pressed the "I'm driving" button on the watch and the application does not start in the simulator:

// Regular location manager
self.standardLocationManager = [[CLLocationManager alloc] init];
self.standardLocationManager.delegate = self;
self.standardLocationManager.desiredAccuracy = self.accuracy;
self.standardLocationManager.activityType = CLActivityTypeAutomotiveNavigation;
self.standardLocationManager.distanceFilter = self.distanceFilter;
[self.standardLocationManager requestWhenInUseAuthorization];

// Significant Location Change location manager
self.significantLocationChangeLocationManager = [[CLLocationManager alloc] init];
self.significantLocationChangeLocationManager.delegate = self;
self.significantLocationChangeLocationManager.activityType = CLActivityTypeAutomotiveNavigation;
[self.significantLocationChangeLocationManager requestWhenInUseAuthorization];

// Start the managers
[self.standardLocationManager startUpdatingLocation];
[self.significantLocationChangeLocationManager startMonitoringSignificantLocationChanges];

      

I'm using MWormhole to "debug" what's going on.

Single delegate method:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways){
        if (manager == self.significantLocationChangeLocationManager) {
            [[WatchController sharedController]notify:@"starting slc loc manager"]; // call MWWormHole to debug
            [self.significantLocationChangeLocationManager startMonitoringSignificantLocationChanges];
        } else if (manager == self.standardLocationManager) {
            [[WatchController sharedController]notify:@"starting standard loc manager"];  // call MWWormHole to debug
            [self.standardLocationManager startUpdatingLocation];
        }
    }
}

      

In this method, the delegate status

is Authorized because I have as a debug message in my watch app.

My questions:

  • Is this what I want to do even in the Apple world?
  • What am I doing wrong? Did I miss something?
  • Is this post clear enough?
+3


source to share





All Articles