2015-09-28 90 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

相关问题