2015-09-21 23 views
6

私のアプリがロックされてロックが解除されたときだけでなく、空になったとき(長い非アクティブ後)に、私のアプリが集中していないのにバックグラウンドで実行されているときに、アプリが集中している間にアプリケーションがバックグラウンドで実行されているときに電話イベントをロック/ロック解除するにはどうすればよいですか?

私は簡単にロック/アンロック/空白のイベントを受け取ることができます。

-(void) startListeningForPhoneLockEvent 
{ 
    NSLog(@"Start listening to lock/unlock and screen-goes-black events."); 

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), 
           (void*)self, 
           lockStateChanged, 
           CFSTR("com.apple.springboard.lockstate"), 
           NULL, 
           CFNotificationSuspensionBehaviorDeliverImmediately); 

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), 
           (void*)self, 
           hasBlankedScreen, 
           CFSTR("com.apple.springboard.hasBlankedScreen"), 
           NULL, 
           CFNotificationSuspensionBehaviorDeliverImmediately); 
} 

とコールバック機能:

static void lockStateChanged(CFNotificationCenterRef center, void*observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 
{ 
    NSLog(@"Lock event received!"); 
} 

static void hasBlankedScreen(CFNotificationCenterRef center, void*observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 
{ 
    NSLog(@"Blanked screen event received!"); 
} 

私は有効にしたバックグラウンドモード:

  • バックグラウンドフェッチ。

ただし、アプリがバックグラウンドに入ると、ロック/アンロック/空白の画面イベントは受信されません。

私は、サウンドの再生、位置の更新などの他のバックグラウンドモードで試してきましたが、バックグラウンドでロック/ロック解除/空白の画面イベントを受け取っていません。

これが実際に可能かどうか、私が間違っているかどうかは分かりません。

最新のXCodeとiOS9 SDKを使用して、iOS9にアップデートされた実際のデバイスでテストしています。

+0

私はSwiftのソリューションも気にしません。 –

+0

アプリケーションでバックグラウンドモードを有効にしても問題が解決しない場合、アプリケーションは実際にバックグラウンドで実行されている必要があります。電話機をロック/ロック解除すると、アプリケーションが実際にバックグラウンドで実行されていることを確認できますか? – user3334059

+0

@SumantHanumante、バックグラウンドでの実行、ロックの聴取、イベントのロック解除に関するアップルの制限はありますか? –

答えて

-1

アプリはバックグラウンドで実行するように設定されていても、実行する作業がない場合は実際には実行されません。それは位置情報の更新とバックグラウンドで実行するために取得するには、these instructions by Rickyに従ってください:

  1. は、私は、ロケーションマネージャーに機能didUpdateLocationsで1分ごとに再起動します。
  2. 私は、(バッテリーを節約するために)シャットダウンする前に、ロケーションマネージャがデバイスから10秒間位置を取得できるようにしました。
以下

部分コード:私は使用して、ロックを聴くとiOS 9を実行しているデバイス上のイベントのロックを解除するためにそこで参照githubのプロジェクトにコードを使用することができました

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ 

for(int i=0;i<locations.count;i++){ 
    CLLocation * newLocation = [locations objectAtIndex:i]; 
    CLLocationCoordinate2D theLocation = newLocation.coordinate; 
    CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy; 
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 

    if (locationAge > 30.0) 
     continue; 

    //Select only valid location and also location with good accuracy 
    if(newLocation!=nil&&theAccuracy>0 
     &&theAccuracy<2000 
     &&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){ 
     self.myLastLocation = theLocation; 
     self.myLastLocationAccuracy= theAccuracy; 
     NSMutableDictionary * dict = [[NSMutableDictionary alloc]init]; 
     [dict setObject:[NSNumber numberWithFloat:theLocation.latitude] forKey:@"latitude"]; 
     [dict setObject:[NSNumber numberWithFloat:theLocation.longitude] forKey:@"longitude"]; 
     [dict setObject:[NSNumber numberWithFloat:theAccuracy] forKey:@"theAccuracy"]; 
     //Add the vallid location with good accuracy into an array 
     //Every 1 minute, I will select the best location based on accuracy and send to server 
     [self.shareModel.myLocationArray addObject:dict]; 
    } 
} 

//If the timer still valid, return it (Will not run the code below) 
if (self.shareModel.timer) 
    return; 

self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager]; 
[self.shareModel.bgTask beginNewBackgroundTask]; 

//Restart the locationMaanger after 1 minute 
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self 
                 selector:@selector(restartLocationUpdates) 
                 userInfo:nil 
                 repeats:NO]; 

//Will only stop the locationManager after 10 seconds, so that we can get some accurate locations 
//The location manager will only operate for 10 seconds to save battery 
NSTimer * delay10Seconds; 
delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self 
               selector:@selector(stopLocationDelayBy10Seconds) 
               userInfo:nil 
               repeats:NO]; 
} 

最新のXCodeここにログがあります:

2015-12-18 13:31:44.777 Location[16185:3796448] startLocationTracking 
2015-12-18 13:31:44.780 Location[16185:3796448] authorizationStatus authorized 
2015-12-18 13:31:44.788 Location[16185:3796448] Start listening to lock/unlock and screen-goes-black events. 
2015-12-18 13:31:44.834 Location[16185:3796448] locationManager didUpdateLocations 
2015-12-18 13:31:44.837 Location[16185:3796448] started master task 1 
2015-12-18 13:31:45.197 Location[16185:3796448] locationManager didUpdateLocations 
2015-12-18 13:31:48.079 Location[16185:3796448] Blanked screen event received! 
2015-12-18 13:31:48.215 Location[16185:3796448] Lock event received!
+0

誰かがこの答えが落ちた理由を説明できますか?私はコメントを追加しようとした、それは私が十分なカルマを持っていなかったと答えのみを追加することができた – user3272750

関連する問題