4

How to get the Xcode project root directory?

Example: /Users/username/MyProjects/Project/. similar to ${SRCROOT}

I checked through FileManager urls and couldn't find the path.

Goal

I am running tests and would like to create a log.txt file in the project root directory.

0

1 Answer 1

4

AFAIK there is no direct way to access this, but what you can do is this for simulator builds:

Modify your xcode scheme to add an environment var SRCROOT with the value ${SRCROOT}

In your code, get the system environment using ProcessInfo:

let srcroot = ProcessInfo.processInfo.environment["SRCROOT"]
Sign up to request clarification or add additional context in comments.

3 Comments

This will not let a simulator app to save a file on Mac
Did you try it? let url = URL(fileURLWithPath: srcroot + "/dummy"); try "foo".data(using: .utf8)?.write(to: url) just created a file on my machine.
I tried to interact with Mac file system via FileManager and it didn't work. I didn't try your way. Ok, thanks, it's good to know that it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.