Detect Which App Is Currently Running on iOS Using Sysctl

Programmatically detect which iOS application is visible to user

Thanks to @JohnWoods in the comment section of the question, I was able to find the solution on a related question.

The solution I used and that worked for me was here.

Can we retrieve the applications currently running in iPhone and iPad

You can get a list of running processes and from process ids may be you can figure out which ones are system processes and which one are 3rd party apps, but anyway I don't believe you can use it in application for appstore. (code taken from here)

- (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;
}:

iOS - detect if app is running from Xcode

You can check if a debugger is attached (probably, but not definitely, Xcode) using sysctl. Here's how HockeyApp does it:

#include <Foundation/Foundation.h>
#include <sys/sysctl.h>

/**
* Check if the debugger is attached
*
* Taken from https://github.com/plausiblelabs/plcrashreporter/blob/2dd862ce049e6f43feb355308dfc710f3af54c4d/Source/Crash%20Demo/main.m#L96
*
* @return `YES` if the debugger is attached to the current process, `NO` otherwise
*/
- (BOOL)isDebuggerAttached {
static BOOL debuggerIsAttached = NO;

static dispatch_once_t debuggerPredicate;
dispatch_once(&debuggerPredicate, ^{
struct kinfo_proc info;
size_t info_size = sizeof(info);
int name[4];

name[0] = CTL_KERN;
name[1] = KERN_PROC;
name[2] = KERN_PROC_PID;
name[3] = getpid(); // from unistd.h, included by Foundation

if (sysctl(name, 4, &info, &info_size, NULL, 0) == -1) {
NSLog(@"[HockeySDK] ERROR: Checking for a running debugger via sysctl() failed: %s", strerror(errno));
debuggerIsAttached = false;
}

if (!debuggerIsAttached && (info.kp_proc.p_flag & P_TRACED) != 0)
debuggerIsAttached = true;
});

return debuggerIsAttached;
}

Detect which app is in foreground on iOS9 without jailbreak

Unfortunately, I was looking for a similar solution and have come to the conclusion that, at least at this point in time, there are no known methods that will allow you to determine app usage on iOS. Even the MDM providers such as Good, Airwatch, MobileIron, etc. don't seem to be able to do this. If anyone is able to come up with a solution, I'd love to see it.

For now, however, I think we would have seen a solution if someone had one that worked on the latest iOS.

Detect how many apps are in running mode on device

I think in a non-jailbreak app you can not do this.

However for a jailbroken app you can do : Return a list of running background apps/processes in iOS

Also: Detect which app is currently running on iOS using sysctl

EDIT:

As this app in iTunes does the same as you need, so you can go with above.



Related Topics



Leave a reply



Submit