如何从system();获取日志? [英] How to get the log from system();?
问题描述
有没有办法从system()获取日志;就像我做system("open com.apple.nike");
一样,我应该得到Couldn't open application: com.apple.nike. Reason: 8, application disabled or restricted
.这将在我的iOs 7设备上运行
谢谢
// 这是新代码,但无法正常工作,我会得到
*** 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);
我知道这不是完全,但也许有更好的方法.
如果要运行命令(如 这会将打开命令的输出与系统日志文件中的其他任何内容分开. 要使用它,您需要下载NSTask.h标头,并将其包含在您的项目中. 这是一个旧版本,但我敢打赌可能有效. is there a way to get the log from system(); so like when I do Thanks EDIT://
This is the new code, but it wont work, I'll get
I know this isn't exactly what you asked, but perhaps there's a better way. If you want to run a command (like For example, here's an example of using This will keep the output from your open command separate from any other stuff in the system log file. To use it, you'll need to download the NSTask.h header, and include it in your project. Here's an old version, but I bet it still probably works. 这篇关于如何从system();获取日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!open com.apple.nike
),我认为使用NSTask
是iOS上的私有API,但是与OS X上存在的许多API一样,它们实际上可以在iOS上使用(只是不要假设Apple允许它们在App Store中使用!). /p>
system("open com.apple.nike");
I should get Couldn't open application: com.apple.nike. Reason: 8, application disabled or restricted
. This will run on my iOs 7 Device *** 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
), I think using NSTask is actually the best way to do that programmatically. NSTask
will allow you to run commands just like system()
, but has good support for handling the standard output from those commands, without having to do file I/O on the system log file.NSTask
to list directory contents (ls -altr
), and capture the output in a 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
is a private API on iOS, but as is the case with many APIs that exist on OS X, they actually are available on iOS (just don't assume Apple allows them in the App Store!).