Help us understand the problem. What is going on with this article?

【備忘録】Objective-CでCMPedometerで日別に歩数を取得してみた

More than 3 years have passed since last update.

初めての投稿です。
swiftはかじってはいますが、いまいち分かっていないので
今回は、Objective-cで最近話題のCMPedometerをいじってみたいと思います。

まずは、前日の日付を取得

NSDate* backDate = [NSDate dateWithTimeIntervalSinceNow:-i*24*60*60];

次に取得する日別で日付を取得

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];    // 日付だけ取得できるフォーマットで情報を抽出。
NSString* backDateString = [dateFormatter stringFromDate:backDate];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];   // 時間を含めたフォーマットで日付を設定する。
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate* startDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 00:00:00", backDateString]]; // 前日の0:00:00のNSDateが完成。
NSLog(@"%d日前のstart日付:%@",i,startDate);
NSDate* endDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 23:59:59", backDateString]]; // 前日の23:59:59のNSDateが完成。
NSLog(@"%d日前のend日付:%@",i,endDate);

そして、取得

CMPedometer *pedometer = [[CMPedometer alloc]init];
[pedometer queryPedometerDataFromDate:startDate toDate:endDate withHandler:^(CMPedometerData *pedometerData, NSError *error){
        // step数の取得
        NSNumber *step = pedometerData.numberOfSteps;
        NSLog(@"%d日前:step = %@", i,step);
        [mstr appendFormat:@"%d日前:step = %@\n", i,step];
        if (error) {
                NSLog(@"%d日前:error = %@", i,error);
         }
}];

しかし、、、
取得した値は全部null
こんなエラーが出た
Error Domain=CMErrorDomain Code=103 "The operation couldn’t be completed. (CMErrorDomain error 103.)"
調べたところデータがないと出るらしい。。本当か?
https://hirooka.pro/?p=4739
とりあえず、歩数カウントをしたままで
日付更新を待って再度いじってみよう

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした