I'm programming an iOS app for jailbroken devices running iOS 12 or newer in Swift.
It is a package manager and in order to install packages I need to run a command which is dpkg -i [PACKAGE_ID] control
.
In order to achieve that I made the following function:
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
}
When I want to run the command, I call my function like this:
task(launchPath: "/usr/libexec/Thunderbolt/supersling", arguments: "/usr/bin/dpkg -i" + packageID + "control")
But it keeps giving me the following error:
'CommandLine' cannot be constructed because it has no accessible initializers
I've been searching on the internet for this error, and I've read that you cannot run CommandLine()
in iOS, but I know it is possible to run commands.
I've been searching on the internet on how to do this and found something for 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];
This does the exact same thing I do but in Objective-C.
Why does Xcode allow this Objective-C code to be run but the Swift one doesn't? Is there any other way to run commands for Swift?
Thanks in advance.
EDIT: I've discovered that the Objective-C code imports it's own NSTask.h
header file:
//#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
Can I use this with Swift? If possible, how could I do that?