プロセス pid から他のアプリケーションのディレクトリ パスを取得するにはどうすればよいですか?</p>
iOS には proc_pidpath 呼び出しがないようです。
以下は iOS で動作しsysctl
、Activity Monitor Touch などのアプリケーションが App Store で使用しているものを使用するため、Apple に受け入れられるはずです。ただし、パスを取得した後にそのパスで何をしようとしているかは、Apple に受け入れられない可能性があります。アプリを App Store に提出するつもりがない場合は、おそらく問題ありません。
- (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);
}
iOS では、それよりも少し制限があります。アプリケーション間でファイルを共有したい場合は、iCloud の使用を検討してください。アプリが属する開発者 ID が同じであれば、異なるプラットフォームや異なるアプリにまたがってファイルにアクセスできます。Ray Wenderlich が、これに関する役立つチュートリアルを書きました。幸運を!