gpt4 book ai didi

ios - 以编程方式从iOS系统上的进程pid获取其他应用程序的目录路径?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:07:56 24 4
gpt4 key购买 nike

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

似乎在 iOS 上没有 proc_pidpath 调用。

最佳答案

以下适用于 iOS 并使用 sysctl,App Store 中使用的应用程序(如 Activity Monitor Touch)应该会被 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 - 以编程方式从iOS系统上的进程pid获取其他应用程序的目录路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10160013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com