CoreMotion运动与健身之监听用户运动状态

#import <CoreMotion/CoreMotion.h>


// 运动与健身

- (void)motion

{

    if ([CMMotionActivityManager isActivityAvailable])

    {

        CMMotionActivityManager *motionActivityManager = [[CMMotionActivityManager alloc]init];

        

        [motionActivityManager startActivityUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMotionActivity *activity) {

            

            NSLog(@"confidence=%@  unknown=%@   walking=%@  stationary=%@  running=%@   cycling=%@  automotive=%@  startDate==%@", @(activity.confidence), @(activity.unknown), @(activity.walking), @(activity.stationary), @(activity.running), @(activity.cycling), @(activity.automotive), activity.startDate);

        }];

        

        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

        NSDateComponents *dateCom = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];

        

        NSDate *endDate = [calendar dateFromComponents:dateCom];

        

        [dateCom setHour:0];

        [dateCom setMinute:0];

        [dateCom setSecond:0];

        

        NSDate *startDate = [calendar dateFromComponents:dateCom];

        [motionActivityManager queryActivityStartingFromDate:startDate toDate:endDate toQueue:[NSOperationQueue mainQueue] withHandler:^(NSArray<CMMotionActivity *> * _Nullable activities, NSError * _Nullable error) {

            for (CMMotionActivity *activity in activities)

            {

                NSLog(@"query confidence=%@  unknown=%@   walking=%@  stationary=%@  running=%@   cycling=%@  automotive=%@  startDate==%@", @(activity.confidence), @(activity.unknown), @(activity.walking), @(activity.stationary), @(activity.running), @(activity.cycling), @(activity.automotive), activity.startDate);

            }

        }];

         

        [motionActivityManager stopActivityUpdates];

        

    } else

    {

        NSLog(@"运动与健身 不可用");

    }

}


上一篇:HealthKit之实战锚点读取


下一篇:React Native热更新方案