5

Is there a way that I can know how long the apps in the foreground has been running? I have three possible solutions in mind:

I. use battery consumption and battery consumption rate (iOS 8 and later tell you the battery usage of the app, but the batter consumption will be difficult to handle)

II. use system processes monitor

III. use Apple's diagnostics logs. This approach is quite "backdoor." Plus I am not sure if Apple allows us to use the information or not.

Can someone tell me if any of the above solution is realistic? If not, I want to know is it possible to find out the duration of a running app on iOS at all?

angerhang
  • 327
  • 4
  • 13
  • Not your own app, but other apps? How will you even know what those apps are? – Marcus Adams Feb 06 '15 at 19:53
  • 1
    Yes other apps. There are apps like batteryDoctor that tell you the battery consumption of different apps, so it is possible. – angerhang Feb 06 '15 at 19:57
  • 1
    Did you succeed in tracking running time of other apps?whats your update? – Imran Jul 23 '15 at 06:32
  • No I didn't. After some more research, I noticed even if tracking time of other apps is technically possible, it goes against Apple's policy. The only way I saw that could do this, is to jailbreak which can bypass some permission issues and constraints, which I don't know exactly how, but I saw an a functional app somewhere. – angerhang Jul 23 '15 at 09:08

2 Answers2

5

You can't access any data like that from other apps. Because every app works in its own sandbox and so you don't have the possibility to do that. You can't know because of the battery it consumes, how long an app is running. It depends on the frameworks it uses etc. Also if it's a game with high resolution graphics etc.

So: None of your ideas are possible.

Christian
  • 22,585
  • 9
  • 80
  • 106
2

With sysctl, you can get many informations about running processes. See in this code, you can find all running processes and also the started time of each process. This code is not private API so it should be accepted by Apple in case of posting it in Apple Store. Take a look in 'struct kinfo_proc' in sysctl.h, you will find useful infos. I don't know how to detect if a process is in foreground or background. I can find just the start_time, then to calculate running time. However, as you run this application in foreground, other processes are likely in background, isn't it?

#import <sys/sysctl.h>

- (NSArray *)runningProcesses
{
    int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
    size_t miblen = 4;

    size_t size;
    int st = sysctl(mib, miblen, NULL, &size, NULL, 0);

    struct kinfo_proc *process = NULL;
    struct kinfo_proc *newprocess = NULL;

    do {
        size += size / 10;
        newprocess = realloc(process, size);

        if (!newprocess) {
            if (process) {
                free(process);
            }

            return nil;
        }

        process = newprocess;
        st = sysctl(mib, miblen, process, &size, NULL, 0);
    } while (st == -1 && errno == ENOMEM);

    if (st == 0) {
        if (size % sizeof(struct kinfo_proc) == 0) {
            int nprocess = size / sizeof(struct kinfo_proc);

            if (nprocess) {
                NSMutableArray * array = [[NSMutableArray alloc] init];

                for (int i = nprocess - 1; i >= 0; i--) {
                    NSString *processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
                    NSString *processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];

                    struct timeval t = process[i].kp_proc.p_un.__p_starttime;
                    long ms = t.tv_sec;
                    NSDate *startDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:ms];

                    NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, startDate, nil]
                                                                       forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName",@"StartDate", nil]];
                    [array addObject:dict];
                }

                free(process);
                return array;
            }
        }
    }

    return nil;
}
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44