iOS: Change device capabilities with a postProcessBuild step

So, this one solves a bug in Unity 2017.4.2f2 that causes the armv7 device capability to be added to a 64 bit only build. We actually want arm64 instead of armv7 so I’ve setup this little hack to handle the fix

 

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System;

// This has been added as a patch to solve a bug in Unity 2017.4.2f2 that incorrectly adds armv7 instead of arm64 to the plist
// ...and no, I don't like doing this.

public class PatchArchitecture
{
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS)
return;

#if UNITY_IOS

string filePath = Path.Combine(pathToBuiltProject, "Info.plist");
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(filePath));
PlistElementDict rootDict = plist.root;

// Get or create array to manage device capabilities
const string capsKey = "UIRequiredDeviceCapabilities";
PlistElementArray capsArray;
PlistElement element;

capsArray = rootDict.values.TryGetValue(capsKey, out element) ? element.AsArray() : rootDict.CreateArray(capsKey);

// Remove the armv7
const string arch = "armv7";
capsArray.values.RemoveAll(x => arch.Equals(x.AsString()));

// Add the arm64
capsArray.AddString("arm64");
File.WriteAllText(filePath, plist.WriteToString());

#endif // #if UNITY_IOS
}
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s