title: How can I run Command Line commands or tasks with Swift in iOS?
body: <p>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 <code>dpkg -i [PACKAGE_ID] control</code>.</p>
  
  <p>In order to achieve that I made the following function:</p>
  
  <pre><code>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
  
  }
  </code></pre>
  
  <p>When I want to run the command, I call my function like this:</p>
  
  <pre><code>task(launchPath: "/usr/libexec/Thunderbolt/supersling", arguments: "/usr/bin/dpkg -i" + packageID + "control")
  </code></pre>
  
  <p>But it keeps giving me the following error:</p>
  
  <pre><code>'CommandLine' cannot be constructed because it has no accessible initializers
  </code></pre>
  
  <p>I've been searching on the internet for this error, and I've read that you cannot run <code>CommandLine()</code> in iOS, but I know it is possible to run commands.</p>
  
  <p>I've been searching on the internet on how to do this and found something for Objective-C:</p>
  
  <pre><code>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];
  </code></pre>
  
  <p>This does the exact same thing I do but in Objective-C.</p>
  
  <p>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?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong>: I've discovered that the Objective-C code imports it's own <code>NSTask.h</code> header file:</p>
  
  <pre><code>//#import &lt;Foundation/Foundation-Structs.h&gt;
  @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
  </code></pre>
  
  <p>Can I use this with Swift? If possible, how could I do that?</p>