2015-09-28 60 views
2

如何跟蹤iPhone的總使用量,即使我們的應用程序在後臺運行(未強制終止)。最近我遇到了應用程序,即 Moment此應用程序確實會跟蹤您的iPhone使用情況,即使此應用程序在後臺運行。實際上他們正在使用位置服務來獲得執行時間。我的問題是當他們獲得執行時間時,他們如何確定用戶的iPhone屏幕鎖定或解鎖?如何跟蹤'Moment'應用程序的iPhone使用情況?

我有代碼來檢查,如果屏幕鎖定或解鎖

-(void)registerAppforDetectLockState { 

    int notify_token; 
    notify_register_dispatch("com.apple.springboard.lockstate", &notify_token,dispatch_get_main_queue(), ^(int token) { 

     uint64_t state = UINT64_MAX; 
     notify_get_state(token, &state); 

     if(state == 0) { 
      NSLog(@"unlock device"); 

      UIAlertView *errorAlert = [[UIAlertView alloc] 
             initWithTitle:@"unlock device" message:@"test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [errorAlert show]; 


     } else { 
      NSLog(@"lock device"); 

      UIAlertView *errorAlert = [[UIAlertView alloc] 
             initWithTitle:@"lock device" message:@"test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [errorAlert show]; 

     } 

    }); 
} 

但是當應用程序在前臺運行該代碼只工作。

回答

1

只有當應用程序在前臺運行時,您的代碼才能正常工作,因爲當應用程序進入後臺時它不會被調用,您必須使用其中一個background mode來喚醒您的應用程序才能執行代碼。

+0

背景模式,鏈接是不開放 –

+0

請嘗試此鏈接.. https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/ doc/uid/TP40007072-CH4-SW23 – Imran

+0

您可以從項目設置的功能選項卡中聲明背景模式。啓用背景模式選項可將UIBackgroundModes鍵添加到應用的Info.plist文件中。選擇一個或多個複選框會將相應的背景模式值添加到該鍵。 – Imran