# Unity xcode api

**URL:** https://discussions.unity.com/t/unity-xcode-api/557603
**Category:** Mobile
**Tags:** iOS
**Created:** [November 20, 2014, 11:50pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603 "2014-11-20T23:50:59Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Jeffom](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jeffom/32/258045_2.png) [@Jeffom](https://discussions.unity.com/u/Jeffom)
#### Post date: [November 20, 2014, 11:50pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/1 "2014-11-20T23:50:59Z")

</div>

I’m trying to figure out how to use this,

[https://bitbucket.org/Unity-Technologies/xcodeapi](https://bitbucket.org/Unity-Technologies/xcodeapi)

Has anyone figured out yet?

---

<div class="post-metadata">

### Author: ![cbaltzer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cbaltzer/32/235835_2.png) [@cbaltzer](https://discussions.unity.com/u/cbaltzer)
#### Post date: [November 21, 2014, 12:03am UTC](https://discussions.unity.com/t/unity-xcode-api/557603/2 "2014-11-21T00:03:10Z")

</div>

Nice, didn’t know that existed.

Looks like they’re using it here, for example: [https://bitbucket.org/Unity-Technologies/iosnativecodesamples/src/a0bc90e7d6358e456caf25d717134864218740a7/NativeIntegration/Misc/UpdateXcodeProject/Assets/Editor/XcodeProjectUpdater.cs?at=stable](https://bitbucket.org/Unity-Technologies/iosnativecodesamples/src/a0bc90e7d6358e456caf25d717134864218740a7/NativeIntegration/Misc/UpdateXcodeProject/Assets/Editor/XcodeProjectUpdater.cs?at=stable)

---

<div class="post-metadata">

### Author: ![Umai](https://avatars.discourse-cdn.com/v4/letter/u/9dc877/32.png) [@Umai](https://discussions.unity.com/u/Umai)
#### Post date: [March 2, 2015, 3:54am UTC](https://discussions.unity.com/t/unity-xcode-api/557603/3 "2015-03-02T03:54:15Z")

</div>

Someone wrote an example usage here:

[http://cflat-inc.hatenablog.com/entry/2015/01/05/074442](http://cflat-inc.hatenablog.com/entry/2015/01/05/074442)

Quoted code:

```csharp
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode; // ←さっきいれたXcodeAPI
using System.Collections;
using System.IO;

public class XcodeProjectMod : MonoBehaviour
{

    // ちょっとしたユーティリティ関数（http://goo.gl/fzYig8を参考）
    internal static void CopyAndReplaceDirectory(string srcPath, string dstPath)
    {
        if (Directory.Exists(dstPath))
            Directory.Delete(dstPath);
        if (File.Exists(dstPath))
            File.Delete(dstPath);

        Directory.CreateDirectory(dstPath);

        foreach (var file in Directory.GetFiles(srcPath))
            File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file)));

        foreach (var dir in Directory.GetDirectories(srcPath))
            CopyAndReplaceDirectory(dir, Path.Combine(dstPath, Path.GetFileName(dir)));
    }

    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    {
        if (buildTarget == BuildTarget.iPhone)
        {
            string projPath = PBXProject.GetPBXProjectPath(path);
            PBXProject proj = new PBXProject();
      
            proj.ReadFromString(File.ReadAllText(projPath));
            string target = proj.TargetGuidByName("Unity-iPhone");

            // システムのフレームワークを追加
            proj.AddFrameworkToProject(target, "AssetsLibrary.framework", false);
      
            // 自前のフレームワークを追加
            CopyAndReplaceDirectory("Assets/Lib/mylib.framework", Path.Combine(path, "Frameworks/mylib.framework"));
            proj.AddFileToBuild(target, proj.AddFile("Frameworks/mylib.framework", "Frameworks/mylib.framework", PBXSourceTree.Source));

            // ファイルを追加
            var fileName = "my_file.xml";
            var filePath = Path.Combine("Assets/Lib", fileName);
            File.Copy(filePath, Path.Combine(path, fileName));
            proj.AddFileToBuild(target, proj.AddFile(fileName, fileName, PBXSourceTree.Source));

            // Yosemiteでipaが書き出せないエラーに対応するための設定
            proj.SetBuildProperty(target, "CODE_SIGN_RESOURCE_RULES_PATH", "$(SDKROOT)/ResourceRules.plist");

            // フレームワークの検索パスを設定・追加
            proj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");
            proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/Frameworks");
      
            // 書き出し
            File.WriteAllText(projPath, proj.WriteToString());
        }
    }
}

```

I’m trying to figure this all out at the moment. I need to add frameworks, edit the pch file, add additional linker flags, some files and directories, add url schemes, and so on… so far I could do some of it with help from above code. I might post more on it later in case I figure it all out…

Some things:

- they got the “projPath” wrong for me, I had to change “Unity-iPhone” into “Unity-iPhone.xcodeproj” in “projPath”.

- There is a class for handling plist in there, but I’ll just use plistbuddy anyway, as it’s part of OS X.

- So far I fail at adding a folder “by reference” recursively… I have a folder which contains several subfolder of which some contains extra .framework:s and other libs and files. Somehow adding a folder always adds it as “real” folder… I need it to be a reference (yellow folder in Xcode, not blue).

- I actually have to add the appcontroller.m (replace actual code inside didFinishLaunching… and more) file on every build. It’s a real pain in the neck and I guess it would be hard to pull that off even with these tools… sigh.

---

<div class="post-metadata">

### Author: ![georgepiva](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/georgepiva/32/578277_2.png) [@georgepiva](https://discussions.unity.com/u/georgepiva)
#### Post date: [March 13, 2015, 2:21pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/4 "2015-03-13T14:21:11Z")

</div>

Hi @Umai , I am facing the same issues that you reported above. Did you find some answers? My main issues are:

> [@](#):
>
> - So far I fail at adding a folder “by reference” recursively… I have a folder which contains several subfolder of which some contains extra .framework:s and other libs and files. Somehow adding a folder always adds it as “real” folder… I need it to be a reference (yellow folder in Xcode, not blue).

> [@](#):
>
> - I actually have to add the appcontroller.m (replace actual code inside didFinishLaunching… and more) file on every build. It’s a real pain in the neck and I guess it would be hard to pull that off even with these tools… sigh

- Adding compiler flags to files like `-fno-objc-arc`;

- Conditional compilation in order to add `iPhoneOS` or `iPhoneSimulator` platforms specific lib versions.

Did some of you guys try to use one of the next alternatives?

- onevcat/XUPorter - [GitHub - onevcat/XUPorter: Add files and frameworks to your Xcode project after it is generated by Unity 3D.](https://github.com/onevcat/XUPorter)
- dcariola/XCodeEditor-for-Unity - [GitHub - dcariola/XCodeEditor-for-Unity: XCode 4 project editor for Unity3D postprocess.](https://github.com/dcariola/XCodeEditor-for-Unity)
- kronenthaler/mod-pbxproj - [GitHub - kronenthaler/mod-pbxproj: A python module to manipulate XCode projects](https://github.com/kronenthaler/mod-pbxproj)

---

<div class="post-metadata">

### Author: ![psydent](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@psydent](https://discussions.unity.com/u/psydent)
#### Post date: [March 16, 2015, 7:07pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/5 "2015-03-16T19:07:35Z")

</div>

Use mod-pbxproj. It says it supports adding a folder “by reference” recursively. The Unity way is dumping everything into Assets/Plugins/iOS. You’d still have to add your frameworks to the project so they can be linked.

You can call mod-pbxproj using (shorthand, missing exception handling, logging output, etc.):  
Process myProcess = new Process();  
myProcess.StartInfo.FileName = “python”;  
myProcess.StartInfo.Arguments = ?; // the path to your python file  
myProcess.StartInfo.UseShellExecute = false;  
// set anything else  
myProcess.Start();  
myProcess.WaitForExit();

For compiler flags per file, in your python script you need to make a filelist and can do:  
project.apply\_mods({‘compiler\_flags’: {‘-fno-objc-arc’: filelist}})  
To make the file list I start with filelist = os.listdir(srcDir) and add/remove. I wrote this for a one-off task so apologies it’s not generalized.

For adding code each time, you should patch it in using a postprocess.

Unity includes a different lib based on if you’re targeting iPhoneOS or iPhoneSimulator so just read that value and add the correct platform specific libs and defines.

---

<div class="post-metadata">

### Author: ![David-Berger](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/david-berger/32/44177_2.png) [@David-Berger](https://discussions.unity.com/u/David-Berger)
#### Post date: [March 17, 2015, 4:03pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/6 "2015-03-17T16:03:51Z")

</div>

If you like you can check out the Native Library Demo in the [Unity Cloud Build Demos](http://forum.unity3d.com/threads/ucb-demos.306687/). It is not directly connected as it an example to add a native library to XCode via post process using the XCode Manipulation API, targeting Unity Cloud Build, but the general concept is the same. Although, there are differenced between Unity 4.x and 5.x as the plugin folders are handled differently.

---

<div class="post-metadata">

### Author: ![povilas](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/povilas/32/44175_2.png) [@povilas](https://discussions.unity.com/u/povilas)
#### Post date: [March 17, 2015, 6:40pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/7 "2015-03-17T18:40:11Z")

</div>

> [@psydent](#):
>
> Use mod-pbxproj.

Please don’t use mod-pbxproj. It does not handle project files correctly. The problem does not arise when opening the project with Xcode though, as it can read slightly corrupted files just fine. However when postprocess steps of various plugins interact with each another, the build sometimes breaks in very strange ways that are hard to understand.

Use Xcode API provided by Unity. Unfortunately its documentation is not available on the website for some reason at the moment. I’ll look into this issue and make sure the documentation is available there in the following days.

---

<div class="post-metadata">

### Author: ![psydent](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@psydent](https://discussions.unity.com/u/psydent)
#### Post date: [March 21, 2015, 2:30am UTC](https://discussions.unity.com/t/unity-xcode-api/557603/8 "2015-03-21T02:30:56Z")

</div>

> [@](#):
>
> Please don’t use mod-pbxproj. It does not handle project files correctly.

mod-pbxproj does handle project files correctly. I’d be happy to re-evaluate if you have an example for which it breaks. But we have over a dozen iOS build jobs using it without any issues.

When people use mod-pbxproj incorrectly, then it can break. But all those fixes are outside of the mod-pbxproj script. I’ve had to fix the postprocess steps of some plugins and it’s usually because they made bad assumptions.

The Xcode API provided by Unity is less clear than mod-pbxproj. Having the documentation would be a big help in re-evaluating if it’s a better solution. One thing that sticks out is that you have to specify more things when using it, which makes it more prone to breaking/being used incorrectly than mod-pbxproj.

---

<div class="post-metadata">

### Author: ![robot-ink1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/robot-ink1/32/188832_2.png) [@robot-ink1](https://discussions.unity.com/u/robot-ink1)
#### Post date: [April 21, 2015, 10:42pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/9 "2015-04-21T22:42:02Z")

</div>

Any updates on this? Any plans on more documentation? I use mod-pbxproj as well but if that has the potential to cause problems then I’d like to switch over to a cleaner method.

I used the example files to add links to frameworks, which works fine, but now I tried copying a plugin (which doesn’t copy due to a bug in Unity 5 apparently) and the resulting project can’t link to the plugin. It builds when I drag the file in by hand however… any ideas? When copying by hand xcode copies the file and adds it to a group as well as assigns a target, i’m trying to do the same procedure in code but kind of stabbing in the dark…

```auto
var fileName = "metaiosdk";
var filePath = Path.Combine("Assets/Plugins/iOS", fileName);
File.Copy(filePath, Path.Combine(path, "Libraries/" +fileName));
proj.AddFileToBuild(target, proj.AddFile( "Libraries/" + fileName, "Libraries/" + fileName, PBXSourceTree.Source));

```

---

<div class="post-metadata">

### Author: ![mydev555](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mydev555](https://discussions.unity.com/u/mydev555)
#### Post date: [June 11, 2015, 6:24am UTC](https://discussions.unity.com/t/unity-xcode-api/557603/10 "2015-06-11T06:24:25Z")

</div>

I also have a question about the XCode API. For my project I have to include several libraries to build in Xcode, also the gpg framework which has a filesize of 116MB. This means that I cannot include that file directly into my github repo, cause it is too big. Is there a solution for handling such a case with Unity Cloud build and the XCode API?

Can I maybe download the framework file on the fly everytime Unity Cloud builds my app?  
Download link is:

[Play Games C++ SDK Version 1.4.1](https://developers.google.com/games/services/downloads/gpg-cpp-sdk.v1.4.1.zip)

![](https://europe1.discourse-cdn.com/unity/original/4X/8/1/c/81cf0aa1f5744207d6605251b80d679e6e0ea3ff.png)

---

<div class="post-metadata">

### Author: ![povilas](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/povilas/32/44175_2.png) [@povilas](https://discussions.unity.com/u/povilas)
#### Post date: [June 15, 2015, 11:33am UTC](https://discussions.unity.com/t/unity-xcode-api/557603/11 "2015-06-15T11:33:55Z")

</div>

The documentation of the Xcode API is now available here [Unity - Scripting API: PBXProject](http://docs.unity3d.com/ScriptReference/iOS.Xcode.PBXProject.html)

---

<div class="post-metadata">

### Author: ![aihodge](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/aihodge/32/93574_2.png) [@aihodge](https://discussions.unity.com/u/aihodge)
#### Post date: [June 15, 2015, 4:22pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/12 "2015-06-15T16:22:09Z")

</div>

This looks very useful, thanks!

---

<div class="post-metadata">

### Author: ![Keepabee](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/keepabee/32/93937_2.png) [@Keepabee](https://discussions.unity.com/u/Keepabee)
#### Post date: [June 17, 2015, 1:31pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/13 "2015-06-17T13:31:59Z")

</div>

Haha, I love it! That Japanese-commented code example, of which I have no idea what they are trying to do or why, seems to be one of the best Unity code examples I’ve ever read!

Having my own problems to solve and advert plugin -specific XCode setup requirements to follow, I can just look at how the original programmer has used paths, property names and the methods to create solutions.

Yeah, sorry but the Unity Scripting Reference is once again quite unhelpful. I couldn’t have used the XCode API without looking at the abovementioned example code to see what kind of paths were expected, or the above forum comment mentioning the bad return value for PBXProject.GetProjectPath method, or what these property names are and where I can look them up.

I’m overjoyed to have seen this thread and that example!

---

<div class="post-metadata">

### Author: ![antifuzz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/antifuzz/32/338968_2.png) [@antifuzz](https://discussions.unity.com/u/antifuzz)
#### Post date: [July 7, 2015, 11:56am UTC](https://discussions.unity.com/t/unity-xcode-api/557603/14 "2015-07-07T11:56:52Z")

</div>

I’ve just been trying to get to grips with this Unity Xcode API as well, and come up against the exact same issues as above. PBXProject.GetProjectPath does seem to return the wrong value. You have to change “Unity-iPhone” into “Unity-iPhone.xcodeproj” in “projPath” to get it to work.  
The Unity Docs could seriously do with some good examples adding. This would be a hugely helpful api if it were easier to understand.  
As it is, I’ve gone back to mod\_pbxproj, which works and does the job I need it to.

---

<div class="post-metadata">

### Author: ![BigToe1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigtoe1/32/118814_2.png) [@BigToe1](https://discussions.unity.com/u/BigToe1)
#### Post date: [July 13, 2015, 6:13am UTC](https://discussions.unity.com/t/unity-xcode-api/557603/15 "2015-07-13T06:13:44Z")

</div>

I have started using the Xcode Manipulation API and overall it seems to be good. I do have a question regarding adding in a dynamic library. I am trying to add in “libz.1.2.5.dylib” or maybe just “libz.1.dylib”.

If I add them in through Xcode, I do it through the “Link Binary with Libraries” from Build Phases. If I try to add in the “lib.1.2.5.dylib” or "libz.1.dylib"using the PBXProject.addFrameworkToProject(), it adds the file, but it is in red in Xcode which would mean that it is missing.

_proj.AddFrameworkToProject(target, “libz.1.dylib”, false);_

I can copy it our of its location in the iOS SDK and add it to my Unity project, copy it to my BuildProject directory, then add it to the Frameworks, but this seems really ineloquent.

Any advise for the best way to add in the dynamic libraries to Xcode?

Thanks

---

<div class="post-metadata">

### Author: ![Jeffom](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jeffom/32/258045_2.png) [@Jeffom](https://discussions.unity.com/u/Jeffom)
#### Post date: [July 13, 2015, 11:47am UTC](https://discussions.unity.com/t/unity-xcode-api/557603/16 "2015-07-13T11:47:44Z")

</div>

I’m guessing that you need the full path of the lib, have you tried that?

---

<div class="post-metadata">

### Author: ![BigToe1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigtoe1/32/118814_2.png) [@BigToe1](https://discussions.unity.com/u/BigToe1)
#### Post date: [July 13, 2015, 4:18pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/17 "2015-07-13T16:18:44Z")

</div>

> [@Jeffom](#):
>
> I’m guessing that you need the full path of the lib, have you tried that?

Thanks for the advice. The full path doesn’t work with the AddFrameworkToProject() method, but it does work if I use the AddFileToBuild with the full path…

string libZPath = “/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/libz.1.dylib”;

proj.AddFileToBuild(target, proj.AddFile(libZPath,“Frameworks/libz.1.dylib”,PBXSourceTree.Source));

---

<div class="post-metadata">

### Author: ![povilas](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/povilas/32/44175_2.png) [@povilas](https://discussions.unity.com/u/povilas)
#### Post date: [July 14, 2015, 5:23pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/18 "2015-07-14T17:23:47Z")

</div>

AddFrameworkToProject is meant only for system frameworks, like “Gamekit.framework”. For any other libraries please use AddFile with correct source tree - PBXSourceTree.Source for libraries already copied to within the project folder and PBXSourceTree.Sdk for system libraries. For example,

proj.AddFileToBuild(target, proj.AddFile(“usr/lib/libz.1.dylib”, “Frameworks/libz.1.dylib”, PBXSourceTree.Sdk));

---

<div class="post-metadata">

### Author: ![BigToe1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigtoe1/32/118814_2.png) [@BigToe1](https://discussions.unity.com/u/BigToe1)
#### Post date: [July 14, 2015, 10:35pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/19 "2015-07-14T22:35:39Z")

</div>

> [@povilas](#):
>
> AddFrameworkToProject is meant only for system frameworks, like “Gamekit.framework”. For any other libraries please use AddFile with correct source tree - PBXSourceTree.Source for libraries already copied to within the project folder and PBXSourceTree.Sdk for system libraries. For example,
> 
> proj.AddFileToBuild(target, proj.AddFile(“usr/lib/libz.1.dylib”, “Frameworks/libz.1.dylib”, PBXSourceTree.Sdk));

Thanks for the follow-up. That works for me.

---

<div class="post-metadata">

### Author: ![BigToe1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigtoe1/32/118814_2.png) [@BigToe1](https://discussions.unity.com/u/BigToe1)
#### Post date: [July 14, 2015, 10:53pm UTC](https://discussions.unity.com/t/unity-xcode-api/557603/20 "2015-07-14T22:53:43Z")

</div>

> [@robot ink](#):
>
> Any updates on this? Any plans on more documentation? I use mod-pbxproj as well but if that has the potential to cause problems then I’d like to switch over to a cleaner method.
> 
> I used the example files to add links to frameworks, which works fine, but now I tried copying a plugin (which doesn’t copy due to a bug in Unity 5 apparently) and the resulting project can’t link to the plugin. It builds when I drag the file in by hand however… any ideas? When copying by hand xcode copies the file and adds it to a group as well as assigns a target, i’m trying to do the same procedure in code but kind of stabbing in the dark…

I had a similar problem using the example files and at least solved it on my end.

The example file has the following two lines to set the Framework Search Path…

```csharp
proj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");
            proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/Frameworks");

```

The problem with that is that the first line ]proj.SetBuildProperty(target, “FRAMEWORK\_SEARCH\_PATHS”, “$(inherited)”); wipes out any existing framework search paths that may already have been added to the project by another plugin.

So in my case, I had a framework in my Unity Project under “Plugins/iOS/AdColony.framework”. If I don’t run the Post Processor it appears that Unity will automatically add “Plugins/iOS” to the Framework Search Path in Xcode when the project is built.

If I run the example post processor script above it obliterates the “Plugins/iOS/” from the Frameworks Search Path and I have a link compilation error trying to find the headers and I get this…

```csharp
Lexical or Preprocessor Issue
UnityADC.mm:18:9: 'AdColony/AdColony.h' file not found

```

The solution for me and maybe for you… Just don’t use

```csharp
proj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");

```

It shouldn’t be necessary anyway as the default empty Unity Project already sets the Framework Search Paths to “$(inherited)” by default.

Currently it appears just to be a good way to introduce incompatibility between third party plugins and cause developers a lot of extra hours figuring it out. I wish they would revise the sample.

[Next page](https://discussions.unity.com/t/unity-xcode-api/557603.md?page=2)
