How can I get the directory path of other application from process pid?
Seems that there is no proc_pidpath calls on iOS.
The following works on iOS and uses sysctl
which applications like Activity Monitor Touch are using in the App Store so should be acceptable to Apple. However, what you intend to do with the path once you've got it might not be acceptable to Apple. If you aren't intending to submit your app to the App Store then it is probably a non-issue.
- (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);
}
Things on iOS are a bit more restricted than that. If you wish to share files between applications, consider using the iCloud - it will allow you to access files across different platforms and even different apps, given that the developer id that the apps belong to is the same. Ray Wenderlich wrote a helpful tutorial about this. Good luck!