Programmatically get the directory path of another application from a pid process on an iOS system?

How can I get the directory path of another application from the pid of the process?

There seems to be no proc_pidpath calls in iOS.

+3
source share
2 answers

The following works on iOS and uses sysctlwhich applications, such as Activity Monitor Touch, used in the App Store, should be acceptable to Apple. However, what you intend to do with this path as soon as you receive it may not be acceptable to Apple. If you are not going to submit your application to the App Store, this is probably not a problem.

- (NSString *)pathFromProcessID:(NSUInteger)pid {

    // First ask the system how big a buffer we should allocate
    int mib[3] = {CTL_KERN, KERN_ARGMAX, 0};

    size_t argmaxsize = sizeof(size_t);
    size_t size;

    int ret = sysctl(mib, 2, &size, &argmaxsize, NULL, 0);

    if (ret != 0) {
        NSLog(@"Error '%s' (%d) getting KERN_ARGMAX", strerror(errno), errno);            

        return nil;
    }

    // Then we can get the path information we actually want
    mib[1] = KERN_PROCARGS2;
    mib[2] = (int)pid;

    char *procargv = malloc(size);

    ret = sysctl(mib, 3, procargv, &size, NULL, 0);

    if (ret != 0) {
        NSLog(@"Error '%s' (%d) for pid %d", strerror(errno), errno, pid);            

        free(procargv);

        return nil;
    }

    // procargv is actually a data structure.  
    // The path is at procargv + sizeof(int)        
    NSString *path = [NSString stringWithCString:(procargv + sizeof(int))
                                        encoding:NSASCIIStringEncoding];

    free(procargv);

    return(path);
}
+3
source

iOS . , iCloud - , , , , . Ray Wenderlich . !

0

All Articles