Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run swift script from Xcode iOS project as build phase

Tags:

Here is a simple swift script:

#!/usr/bin/env xcrun swift  import Foundation  let task = NSTask() task.launchPath = "/bin/echo" task.arguments = ["farg1", "arg2"]  let pipe = NSPipe() task.standardOutput = pipe task.launch()  let data = pipe.fileHandleForReading.readDataToEndOfFile() let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)  print(output) 

I added this script as build phase(before 'compile sources phase) in iOS project but XCode failed to build project with error 'undefined NSTask()...' When I added same script in OSX project, XCode build project without any errors.

Question is why XCode is looking for NSTask inside iOS framework(where doesn't exists) instead to run swift script as platform script(like bash)?

Just to add: swift script isn't included into project with other files to compile. It's just added into build phase, even before compilation, of other source files(which are objective-c files) to run it.

Any ideas how to run custom swift script from iOS XCode project when script contains classes which aren't part of iOS framework?

like image 204
xezo Avatar asked Jun 22 '15 13:06

xezo


People also ask

How do I run a custom shell script in Xcode?

1 Select xcodeproj file of you project -> Select Target -> Select Build Phases -> Click on plus button (upper left corner) -> Select New Run Script Phase. 2If you want to run a script while it is being installed on the device then please check a little checkbox just below the script box.


1 Answers

When building for iOS the implicit SDK for xcrun is the iOS SDK, so you have the change it to the Mac OS X SDK with a command line parameter. Change the first line in your script to:

#!/usr/bin/env xcrun --sdk macosx swift 
like image 88
Mats Avatar answered Oct 13 '22 20:10

Mats