Add files to Xcode project through command line ? Use of project.pbxproj file in Xcode? Add files to Xcode project through command line ? Use of project.pbxproj file in Xcode? xcode xcode

Add files to Xcode project through command line ? Use of project.pbxproj file in Xcode?


There is a Ruby API from Cocoapods for editing Xcode projects. It also have an active community of developers:

https://github.com/CocoaPods/Xcodeproj


Another great option, especially for Cordova projects, is to use the XCODE node module: node-xcode; you can easily add it via NPM.

Once in place, you can create an after_prepare hook to modify the pbxproj, injecting custom source files, additional frameworks, etc, on every build. In fact, Cordova itself leverages this module during its own build processes.

Within my solution, I first added the module via npm:

npm install xcode --save-dev

And then I created and after_prepare hook to add extra frameworks into my XCode project:

var xcode = require('xcode'),    fs = require('fs'),    rootdir = process.argv[2],    projectPath = rootdir + '/platforms/ios/my-project/project.pbxproj',    proj = new xcode.project(projectPath);proj.parse(function(err) {    if (err) {        console.log("Oh noes! XCODE project failed to parse:");        console.log(err);    } else {        proj.addFramework('Fabric.framework', {customFramework:true});        proj.addFramework('Crashlytics.framework', {customFramework:true});        proj.addFramework('AdSupport.framework');        proj.addFramework('FacebookSDK.framework', {customFramework:true});        fs.writeFileSync(projectPath, proj.writeSync());        console.log("Updated XCODE project with references to social libs!");    }});

The XCODE module is smart enough to know if the frameworks / files / etc that you are trying to add are already present, and won't try to add them again.


What you are asking to do is not the most straightforward thing. The Xcode pbxproj file format looks like XML, but I think there's quite a few proprietary / undocumented pieces to it (much like everything iOS). As far as I can tell, Xcode doesn't have any way to add files from the command line.

I did find a Python script that you might be able to use to modify Xcode's project files, but it's a few years old and it might be out of date.

Here is the Blog post that talks about it and here is the current GitHub repo (last updated five months ago, as of the date of me typing this answer).

Give this a try and let me know if it works for you.