5

iOS에서 만보계를 구현 중입니다. 앱이 백그라운드 모드 (예 : 기기가 잠겨 있거나 사용자가 집 버튼을 누르는 경우)에 들어가더라도 작동해야한다는 중요한 요구 사항이 있습니다. Nike +, Runtastic Pedometer 등과 같은 App Store에서 이러한 구현을 볼 수 있습니다.Core Motion의 가속도계가 백그라운드 모드로 구현 된 이상한 동작

일부 SO 게시물은 Core Motion 또는 CMMotionManager의 추가 속성이 Required background modes 인 경우 location으로 설정할 수 있음을 확인했습니다.

나는 아래의 코드 빠른 검사를 실행하고 이상한 문제가 발견 : 나는 아이폰 4S와 아이폰 OS 6.1이 코드를 시도

// AppDelegate.m 
- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    if(self.motionManager==nil) { 
     self.motionManager=[[CMMotionManager alloc] init]; 
    } 
    self.motionManager.accelerometerUpdateInterval=1/50; 

    if(self.accelerometerReadings==nil) 
     self.accelerometerReadings=[[NSMutableArray alloc] init]; 

    [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.accelerometerReadings addObject:accelerometerData]; 
     }); 
    } 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    NSLog(@"The number of readings %d",self.accelerometerReadings.count); 

    // do something with accelerometer data... 
} 

합니다. 앱이 시작되면 홈 버튼을 누르고 흔들어서 5 초 동안 걷고 앱을 다시 엽니 다. 이러한 행동은 몇 번 반복됩니다. 내가 얻은 결과는 다음과 같습니다.

2013-02-05 16:41:42.028 [1147:907] readings 0 
2013-02-05 16:41:51.572 [1147:907] readings 444 
2013-02-05 16:42:00.386 [1147:907] readings 1032 
2013-02-05 16:42:08.026 [1147:907] readings 1555 
... 

판독 값의 정확성을 검사하지는 않았지만 빠른 관찰 결과는 이미 일부 문제를 나타냅니다. 이 앱이 백그라운드 모드에서 처음 실행될 때 독서 (또는 때로는 독서가 하나도 없음)가 없습니다.

내가 뭘 잘못하고 있니? 그리고 이것은 만보계를 구현하는 올바른 접근법입니까?

+0

당신은 [accelerometerUpdateInterval] 설정해야합니다 (http://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMMotionManager_Class/Reference/Reference.html#//apple_ref/occ/instp/CMMotionManager/accelerometerUpdateInterval)을'deviceMotionUpdateInterval' 대신'1/50'로 변경하십시오. – Kay

+0

좋은 캐치! 불행하게도 이것은 결과를 바꾸지 않습니다. –

+0

안녕하세요, 문제 해결 방법을 찾으셨습니까? – Axarydax

답변