有没有办法从 system(); 获取日志?就像我做的那样system("open com.apple.nike");
我应该得到 Couldn't open application: com.apple.nike. Reason: 8, application disabled or restricted
.这将在我的 iOs 7 设备上运行
谢谢
编辑:// 这是新代码,但它不会工作,我会得到 p>
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible'
*** First throw call stack:
NSString *bundleID = @"com.apple.nike";
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"sudo"];
[task setArguments: [[NSArray alloc] initWithObjects:[NSString stringWithFormat:@"open %@", bundleID], nil]];
NSPipe *pipe= [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
NSData *data = [file readDataToEndOfFile];
NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"result: %@", output);
最佳答案
我知道这不是完全你问的,但也许有更好的方法。
如果你想运行一个命令(比如open com.apple.nike
),我想使用NSTask实际上是以编程方式执行此操作的最佳方法。 NSTask
将允许您像 system()
一样运行命令,但对处理这些命令的标准输出有很好的支持,而无需在系统日志文件。
例如,下面是一个使用 NSTask
列出目录内容(ls -altr
)并在 NSString
中捕获输出的示例:
- (void) listDir {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/ls"];
[task setArguments: [[NSArray alloc] initWithObjects: @"-altr", nil]];
NSPipe *pipe= [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
NSData *data = [file readDataToEndOfFile];
NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"result: %@", output);
}
这将使您打开命令的输出与系统日志文件中的任何其他内容分开。
NSTask
是 iOS 上的私有(private) API,但与 OS X 上存在的许多 API 一样,它们实际上在 iOS 上可用(只是不要假设 Apple 允许它们在应用程序中使用)存储!)。
要使用它,您需要下载 NSTask.h header ,并将其包含在您的项目中。
Here's an old version ,但我敢打赌它仍然可能有效。
关于ios - 如何从 system(); 获取日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21862935/