Can I write the code in this method to get the M7's data and does it useful while I don't run the app?
-(void)locationManager:(CLLocationManager *)manager didVisit:(CLVisit*)visit
{
}
Yes, you can query CMMotionActivityManager from locationManager:didVisit:
Please note that visits are not reported to your app in real time, in my tests they are be delayed 20 to 60 minutes. That means starting activity monitoring with startActivityUpdatesToQueue:withHandler: makes no sense, as these updates won't tell you what happened during the visit.
However you still can fetch and analyze activity events that happened during the visit using queryActivityStartingFromDate:toDate:toQueue:withHandler:
Keep in mind that locationManager:didVisit: might and most likely will be called while your app is in background mode, thus you have about 10 seconds to query the CMMotionActivityManager and process the data. Since you have no control over the CMMotionActivityManager and there is no guarantee it will process your query in a timely fashion, you may want to invoke beginBackgroundTaskWithExpirationHandler: in your locationManager:didVisit: as well.
@property (nonatomic) UIBackgroundTaskIdentifier bgTask;
@property (nonatomic, strong) CMMotionActivityManager *motionActivityManager;
...
- (void)locationManager:(CLLocationManager *)manager didVisit:(CLVisit *)visit
{
if (visit.arrivalDate && visit.departureDate) {
// use strong self here, as we must end the background task explicitly
self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ {
[self stopBackgroundTask];
}];
[self.motionActivityManager queryActivityStartingFromDate:visit.arrivalDate
toDate:visit.departureDate
toQueue:[NSOperationQueue currentQueue]
withHandler:^(NSArray *activities, NSError *error) {
// handle CMMotionActivity history here
[self stopBackgroundTask];
}];
}
}
- (void) stopBackgroundTask {
if (self.bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
}