2013-05-20 14 views
5

アプリがバックグラウンドで実行されているとき、またはデバイスがスリープモードのときにアクセラレータの更新が必要です。いくつかのアプリはこれを行い、私はそれを動作させることができませんでした。 はこのために私は私のappdelegateにCoreMotion特性を持っており、アプリケーションが位置情報の更新のためのレジスタにapplicationDidEnterBackgroundに私は必要なバックグラウンドモードを入れて、アプリのplistでiOSバックグラウンド/スリープモードでのCoremotionアクセラレータの更新

-(void) startAccelerationUpdates 
{ 

    self.motionManager.deviceMotionUpdateInterval = 0.01; 
    [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] 
             withHandler:^(CMDeviceMotion *motion, NSError *error){ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
     NSLog(@"acceleration x %f", motion.userAcceleration.x); 
     NSLog(@"acceleration y %f", motion.userAcceleration.y); 
     NSLog(@"acceleration z %f", motion.userAcceleration.z); 
     } 
     ); 
     } 
    ]; 
} 

を呼び出します。デバイスをスリープモードまたはバックグラウンドにすると、ログに更新が表示されません。アプリがアクティブになると、coremotionがログオンを開始します。誰でも私のヒントを得た?ありがとうございました。

答えて

0

メインキューにディスパッチしているためです。バックグラウンド活性を有効にするには、グローバルキューに派遣する必要があります

dispatch_queue_t lowQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); 
dispatch_async(lowQueue, ^{ ... 

UPDATEスウィフトバージョンを追加します。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)) { 
    // do some task 
} 

参考:using the same dispatch queue in a method for background processing

+0

はありがとうございますが、派遣でのグローバルキューも動作していません。 – simon

+2

また、あなたのアプリのplistで 'Required background modes'を使いましたか?' item 0'で 'App updates for location updates'を選択しましたか? – Raptor

+0

はい、私はしました。ロケーションのアップデートはバックグラウンド/スリープモードで動作しています。 – simon

関連する問題