我正在构建一个简单的GUI来在Cocoa中运行ADB命令。我发现了一些不同的文章,说明如何使用NSTask运行shell命令,但没有任何特定于ADB,而且我无法理解。
我可以运行简单的ADB命令,例如
- adb devices
- adb reboot
功能
NSString *adbPath = @"~/Android/sdk/platform-tools/adb";
NSString* runADBCommand(NSString *cmd)
{
NSTask *adbDevices = [[NSTask alloc] init];
[adbDevices setLaunchPath:adbPath];
adbDevices.arguments = @[cmd];
NSPipe *pipe;
pipe = [NSPipe pipe];
[adbDevices setStandardOutput:pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[adbDevices launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *adbComOutput;
adbComOutput = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return adbComOutput;
}
拨打
- (void)refreshDevices:(id)sender
{
adbOutput.stringValue = runADBCommand(@"devices");
}
以上工作正常,但是当我传递一个更复杂的论点时:
- (void) getVersion:(id)sender
{
runADBCommand(@"shell cat /system/build.prop | grep incremental");
}
我只是得到控制台输出,好像我刚刚在终端输入adb
。如何打包NSTask的命令和参数以运行ADB命令?