简体   繁体   中英

iOS receive notification when user moving

Ive been struggling with a way to retrieve information periodically from a BT device. My bluetooth device is located in a vehicle typically, so my question is if its possible to use say... (if user traveling > 10km/h) to run a task. Or on major location change.

Is is possible to get a really course location that I would be able to use to get a general idea of wether the user is moving? I only need it to trigger once every couple days(while user is driving). The user never interacts with my app after initial setup.

Thanks.

Implementation of cmyr's suggestion:

CLLocationManager *locationManager;
int badge_count = 0;
- (void)startSignificantChangeUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;

    locationManager.pausesLocationUpdatesAutomatically = YES;

    locationManager.activityType = CLActivityTypeAutomotiveNavigation;

    [locationManager requestAlwaysAuthorization];

    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    locationManager.distanceFilter = 500; // meters

    [locationManager allowDeferredLocationUpdatesUntilTraveled:501 timeout:-1];
    [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
         badge_count++;
         [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge_count];
         NSLog(@"Location Event WOOT!");
     }

Unfortunately I cannot get the event to trigger. I have added Location updated to the apps plist.

The above code is contained inside my app delegate.m file

Core Location has a set of APIs for specifically this use-case, which Apple refers to as Significant Location Change Monitoring .

From the documentation:

The significant-change location service delivers updates only when there has been a significant change in the device's location, such as 500 meters or more.

This API only updates your location when and if you've traveled the specified distance. It does not provide constant updates. If you need constant updates, you will have to use the standard location service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM