Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking an App's running time iOS

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?

like image 742
angerhang Avatar asked Feb 06 '15 19:02

angerhang


2 Answers

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.

like image 79
Christian Avatar answered Sep 19 '22 18:09

Christian


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;
}
like image 29
Duyen-Hoa Avatar answered Sep 19 '22 18:09

Duyen-Hoa