Skip to content

Instantly share code, notes, and snippets.

@Geri-Borbas
Last active March 22, 2020 17:12
Show Gist options
  • Save Geri-Borbas/1ebbc1cf6a77741f56d63d3803e57ba3 to your computer and use it in GitHub Desktop.
Save Geri-Borbas/1ebbc1cf6a77741f56d63d3803e57ba3 to your computer and use it in GitHub Desktop.
Adds iOS Frameworks to Xcode project (on each Unity build).
//
// Copyright (c) 2017 eppz! mobile, Gergely Borbás (SP)
// http://www.twitter.com/_eppz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
public class BuildPostProcessor
{
[PostProcessBuildAttribute(1)]
public static void OnPostProcessBuild(BuildTarget target, string path)
{
if (target == BuildTarget.iOS)
{
// Read.
string projectPath = PBXProject.GetPBXProjectPath(path);
PBXProject project = new PBXProject();
project.ReadFromString(File.ReadAllText(projectPath));
string targetName = project.GetUnityTargetName();
string targetGUID = project.TargetGuidByName(targetName);
AddFrameworks(project, targetGUID);
// Write.
File.WriteAllText(projectPath, project.WriteToString());
}
}
static void AddFrameworks(PBXProject project, string targetGUID)
{
// Frameworks (eppz! Photos, Google Analytics).
project.AddFrameworkToProject(targetGUID, "MessageUI.framework", false);
project.AddFrameworkToProject(targetGUID, "AdSupport.framework", false);
project.AddFrameworkToProject(targetGUID, "CoreData.framework", false);
project.AddFrameworkToProject(targetGUID, "SystemConfiguration.framework", false);
project.AddFrameworkToProject(targetGUID, "libz.dylib", false);
project.AddFrameworkToProject(targetGUID, "libsqlite3.tbd", false);
// Add `-ObjC` to "Other Linker Flags".
project.AddBuildProperty(targetGUID, "OTHER_LDFLAGS", "-ObjC");
}
}
@01F0
Copy link

01F0 commented Mar 1, 2020

Thanks for sharing this. :) What's the license for permissions (commercial use/distribution etc.)?

string targetName = project.GetUnityTargetName();
string targetGUID = project.TargetGuidByName(targetName);

can now be replaced with

string targetGUID = project.GetUnityMainTargetGuid();

https://docs.unity3d.com/ScriptReference/iOS.Xcode.PBXProject.GetUnityTargetName.html

@Geri-Borbas
Copy link
Author

Hey, sure thing! 🙌

It is not that huge piece of intellectual property (mostly documented behaviour), but the Gist is MIT licensed (see header).

@01F0
Copy link

01F0 commented Mar 1, 2020

Alright 👍

@mattbajorek
Copy link

project.ReadFromString(File.ReadAllText(projectPath)); can be simplified with project.ReadFromFile(projectPath); and File.WriteAllText(projectPath, project.WriteToString()); can be simplified with project.WriteToFile(projectPath);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment