我正在用 Swift 为运行 iOS 12 或更高版本的越狱设备编写 iOS 应用程序。
它是一个包管理器,为了安装包,我需要运行一个命令,该命令是dpkg -i [PACKAGE_ID] control
.
为了实现这一点,我做了以下功能:
func task(launchPath: String, arguments: String) {
let task = CommandLine()
task.launchPath = launchPath
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)
return output
}
当我想运行命令时,我像这样调用我的函数:
task(launchPath: "/usr/libexec/Thunderbolt/supersling", arguments: "/usr/bin/dpkg -i" + packageID + "control")
但它一直给我以下错误:
'CommandLine' cannot be constructed because it has no accessible initializers
我一直在互联网上搜索此错误,并且我读到您无法运行CommandLine()
在 iOS 中,但我知道可以运行命令。
我一直在互联网上搜索如何做到这一点,并找到了一些适用于 Objective-C 的内容:
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/libexec/Thunderbolt/supersling"];
[task setArguments:@[@"/usr/bin/dpkg", @"-I", packageID, @"control"]];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task launch];
[task waitUntilExit];
NSFileHandle *read = [pipe fileHandleForReading];
NSData *dataRead = [read readDataToEndOfFile];
NSString *stringRead = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding];
这与我所做的完全相同,只不过是在 Objective-C 中。
为什么 Xcode 允许运行此 Objective-C 代码,但 Swift 不允许运行?
还有其他方法可以运行 Swift 命令吗?
提前致谢。
EDIT:我发现 Objective-C 代码导入了它自己的代码NSTask.h
头文件:
//#import <Foundation/Foundation-Structs.h>
@class NSURL, NSArray, NSDictionary;
@interface NSTask : NSObject
@property (copy) NSURL * executableURL;
@property (copy) NSArray * arguments;
@property (copy) NSDictionary * environment;
@property (copy) NSURL * currentDirectoryURL;
@property (retain) id standardInput;
@property (retain) id standardOutput;
@property (retain) id standardError;
@property (readonly) int processIdentifier;
@property (getter=isRunning,readonly) BOOL running;
@property (readonly) int terminationStatus;
@property (readonly) long long terminationReason;
@property (copy) id terminationHandler;
@property (assign) long long qualityOfService;
+(id)currentTaskDictionary;
+(id)launchedTaskWithDictionary:(id)arg1 ;
+(id)launchedTaskWithLaunchPath:(id)arg1 arguments:(id)arg2 ;
+(id)launchedTaskWithExecutableURL:(id)arg1 arguments:(id)arg2 error:(out id*)arg3 terminationHandler:(/*^block*/id)arg4 ;
+(id)allocWithZone:(NSZone*)arg1 ;
-(void)waitUntilExit;
-(NSURL *)executableURL;
-(id)currentDirectoryPath;
-(void)setArguments:(NSArray *)arg1 ;
-(void)setCurrentDirectoryPath:(id)arg1 ;
-(id)launchPath;
-(void)setLaunchPath:(id)arg1 ;
-(int)terminationStatus;
-(long long)terminationReason;
-(void)launch;
-(BOOL)launchAndReturnError:(id*)arg1 ;
-(void)setCurrentDirectoryURL:(NSURL *)arg1 ;
-(NSURL *)currentDirectoryURL;
-(void)setExecutableURL:(NSURL *)arg1 ;
-(void)interrupt;
-(long long)suspendCount;
-(void)setStandardInput:(id)arg1 ;
-(void)setStandardOutput:(id)arg1 ;
-(void)setStandardError:(id)arg1 ;
-(id)standardInput;
-(id)standardOutput;
-(id)standardError;
-(id)init;
-(NSDictionary *)environment;
-(BOOL)isRunning;
-(BOOL)suspend;
-(BOOL)resume;
-(void)setEnvironment:(NSDictionary *)arg1 ;
-(void)setQualityOfService:(long long)arg1 ;
-(void)setTerminationHandler:(id)arg1 ;
-(int)processIdentifier;
-(id)terminationHandler;
-(long long)qualityOfService;
-(void)terminate;
-(NSArray *)arguments;
@end
我可以将它与 Swift 一起使用吗?如果可以的话,我该怎么做呢?