Is there any way, to know (programmly) the time you spent on some app? And then use that information in your own app? (In IOS 8 you can see the usage of battery per app in persentage, i guess there will be some way to know the duration)
Asked
Active
Viewed 67 times
0
-
But, is there any way to know the time you spent on ANOTHER app? – Max Adamyan Jan 10 '15 at 14:57
1 Answers
0
while your app is running (or using background refresh) you can periodicly check which other processes are running on ios
you can deduce the apps based on the processes and you can deduce timings. It wont be accurate at all but it can provide you with timing 'trends'
on unix you'd use ps. this code is for ios (and works on non-jailbroken devices):
- (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];
NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]
forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];
[processID release];
[processName release];
[array addObject:dict];
[dict release];
}
free(process);
return [array autorelease];
}
}
}
return nil;
}
source for the code: How to get information about free memory and running processes in an App Store approved app? (Yes, there is one!)
-
Thank you for answer! And how can I check what process are running? What does PS mean? – Max Adamyan Jan 10 '15 at 16:47