以编程方式从iOS系统上的进程pid获取其他应用程序的目录路径? [英] Programmatically get the directory path of other application from process pid on iOS system?

查看:127
本文介绍了以编程方式从iOS系统上的进程pid获取其他应用程序的目录路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从进程pid获取其他应用程序的目录路径?

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

似乎iOS上没有proc_pidpath调用。

Seems that there is no proc_pidpath calls on iOS.

推荐答案

以下适用于iOS并使用 sysctl Activity Monitor Touch等应用程序在App Store中使用所以苹果应该接受。但是,一旦你得到它,你打算用这条路做什么可能是苹果公司不能接受的。如果您不打算将您的应用程序提交到App Store,那么这可能不是问题。

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);
}

这篇关于以编程方式从iOS系统上的进程pid获取其他应用程序的目录路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆