3

How to track total usage of iPhone, even if our app is running in background(not forcefully terminated). Recently i have come across app, namely Moment this app does track your iPhone usages, even if this app is running in background. actually they are using location service to get execution time. my question is when they get execution time, how they can be sure if user's iPhone screen lock or unlock ?

i have code to check if screen is lock or unlock

-(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];

        }

    });
}

but this code is working only when app is running in foreground.

joern
  • 27,354
  • 7
  • 90
  • 105
Sheshnath
  • 3,293
  • 1
  • 32
  • 60

1 Answers1

1

Your code is working only when app is running in foreground because when app goes in to background it is not called you have to use one of the background mode to wakeup your application can execute your code .

Stunner
  • 12,025
  • 12
  • 86
  • 145
Imran
  • 2,985
  • 19
  • 33
  • background mode , link is not opening – Sheshnath Sep 28 '15 at 06:58
  • Please try this link.. https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW23 – Imran Sep 28 '15 at 06:59
  • You can declare the background modes from the Capabilities tab of your project settings. Enabling the Background Modes option adds the UIBackgroundModes key to your app’s Info.plist file. Selecting one or more checkboxes adds the corresponding background mode values to that key. – Imran Sep 28 '15 at 07:05
  • 1
    ok, but i think this is not gonna help.background mode is to get execution time for app, using location update i am getting execution time for app so no need to of background mode – Sheshnath Sep 28 '15 at 07:07
  • @Saurabh yes you can as it depends on your use cases what you want to track and your implementation nothing to with MDM that is just distribution channel – Imran May 24 '17 at 08:39
  • @Imran I want to track the application foreground total time in a day, of my device. For example in my phone five application, I want to get all application foreground time, mean how many times I play with the application... is this possible by MDM or other technique? Please help me... Thanks – Saurabh Jain May 24 '17 at 12:02
  • @Imran you want to track the app you develop or all apps ? all you can't but your app you can as I mentioned using MDM or not doesn't matter – Imran May 25 '17 at 08:56