Track iOS app runtime

Is there a way to find out how long the applications in the foreground last? I have three possible solutions:

I am. use battery consumption and battery consumption (iOS 8 and later talk about using the battery in the application, but the consumption of the test signal will be difficult to handle)

II. monitoring system processes

III. Use Apple Diagnostic Logs. This approach is quite a backdoor. Plus, I'm not sure if Apple allows us to use the information or not.

Can someone tell me if any of the above solutions are really realistic? If not, I want to know if it is possible to figure out the duration of the launch of the application on iOS?

+4
source share
2 answers

You cannot access any data from other applications. Since each application runs in its own sandbox, and therefore you have no way to do this. You may not know, due to the battery it consumes, how long the application is running. It depends on the frameworks they use, etc. Also, if it is a game with high-resolution graphics, etc.

So: None of your ideas are possible.

+4
source

sysctl . . , , . API, Apple Apple Store. "struct kinfo_proc" sysctl.h, . , , . start_time, . , , , , , ?

#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;
}
+1

All Articles