- Home /
custom .plist file not added to ios project
Hi, I have a custom .plist file I would like to have unity add to the resulting xcode project. I put it in Assets/Plugins/iOS along with a library (.a) file. The library gets included in the project correctly, but the .plist file is nowhere to be found. How can I get it to be added to the project as a file, just like Info.plist?
Answer by sumiguchi · Nov 10, 2013 at 05:50 AM
Rename you file plist file to have a txt extension.
That didn't seem to work for me. I tried NewFile.plist.txt as well as NewFile.txt. I'm using Unity 4.5.3, is there a new way? I just want to insert a set of custom plist files into the root of my generated xcode project and have it recognized by the build.
Answer by David-Berger · Nov 12, 2015 at 01:17 PM
.plist usually need to be merged with the Info.plist of the iOS project and the .plist files are not uploaded. (Not sure what you need them for except adding info to Info.plist?)
You however can add the information you need via post process steps using the XCode Manipulation API which can be found here: https://bitbucket.org/Unity-Technologies/xcodeapi
Have a look in the forums for examples and how to use if if you are stuck. There is a lot of information available there about it, especially in the iOS and Unity Cloud Build forums.
Answer by $$anonymous$$ · Oct 06, 2016 at 04:19 PM
As an example here's the code I use to set NSCameraUsageDescription. This file must be in an assets Editor/ folder
using UnityEngine;
using System.Collections;
using UnityEditor.Callbacks;
using UnityEditor;
using UnityEditor.iOS.Xcode;
using System;
using System.IO;
using System.Linq;
using Mk.Common;
using System.Collections.Generic;
public class GveBuild {
private const string NSCameraUsageDescription = "NSCameraUsageDescription";
[PostProcessBuildAttribute(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
Debug.Log("GveBuild.OnPostprocessBuild " + target + " " + pathToBuiltProject);
switch (target) {
case BuildTarget.iOS:
string plistFilePath = pathToBuiltProject + Path.DirectorySeparatorChar + "Info.plist";
// 10/5/16 The code that uses Xcode is flagged as missing in editor, but it compiles when we return to Unity and works
// when build is run.
// Q: why doesn't this work? A: 2nd link says it is fixed in 5.4.2, we'll see
// https://issuetracker.unity3d.com/issues/ios-monodevelop-unityeditor-dot-ios-dot-xcode-namespace-isnt-recognised-in-monodevelop
// https://issuetracker.unity3d.com/issues/unity-does-not-include-unityeditor-dot-ios-dot-xcode-in-project-file
// Read the existing plist file
try {
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistFilePath);
Debug.Log("GveBuild.OnPostprocessBuild successfully read " + plistFilePath + ": " + ValueToString(plist.root));
// Add our modifications
if (plist.root.values.ContainsKey(NSCameraUsageDescription)) {
Debug.LogError("GveBuild.OnPostprocessBuild key already set? Do not overwrite: " + NSCameraUsageDescription + " = " + ValueToString(plist.root.values[NSCameraUsageDescription]));
} else {
plist.root.values[NSCameraUsageDescription] = new PlistElementString("Camera is not used by our application (API reference is due to Unity libraries)");
Debug.Log("GveBuild.OnPostprocessBuild added NSCameraUsageDescription = " + ValueToString(plist.root.values[NSCameraUsageDescription]));
}
// Write the modified file
string plistPathNew = plistFilePath + ".new";
plist.WriteToFile(plistPathNew);
// Replace the original file. Note subsequent build steps modify the plist file further (e.g. currently facebook entries are added after this script runs)
if (true) {
// Delete the old file
File.Delete(plistFilePath);
} else {
// Keep the old file for diff while testing
string plistPathOld = plistFilePath + ".orig";
File.Move(plistFilePath, plistPathOld);
}
File.Move(plistPathNew, plistFilePath);
Debug.Log("GveBuild.OnPostprocessBuild successfully updated " + plistFilePath);
} catch (Exception e) {
Debug.LogError("GveBuild.OnPostprocessBuild PList error " + e);
}
break;
default:
// nada
break;
}
}
/// <summary>
/// Utility to get value of a PlistElement as string
/// </summary>
private static string ValueToString(PlistElement element) {
if (element is PlistElementString) {
return ((PlistElementString)element).value;
} else if (element is PlistElementInteger) {
return ((PlistElementInteger)element).value.ToString();
} else if (element is PlistElementBoolean) {
return ((PlistElementBoolean)element).value.ToString();
} else if (element is PlistElementDict) {
Dictionary<string, PlistElement> values;
return ((PlistElementDict)element).values.Select(x => x.Key + " : " + ValueToString(x.Value)).Join();
} else {
return element.ToString();
}
}
}
Hi @$$anonymous$$ ,
I got an issue message from App Store Connect when I was trying to upload an .ipa file built from Unity Cloud Build.
Error $$anonymous$$essage :
$$anonymous$$issing Purpose String in Info.plist File - Your app's code references one or more APIs that access sensitive user data. The app's Info.plist file should contain a NSPhotoLibraryUsageDescription key with a user-facing purpose string explaining clearly and completely why your app needs the data
I was wondering if your script could work for me if I change the NSCameraUsageDescription by NSPhotoLibraryUsageDescription ..
Another question about the script usage.. must that public static function be called from anywhere, otherwise it's an event called automatically when the app is building for iOS platform ? I mean, must I call it from another place, message , etc (Start(), Awake (),...)? ?
Thank you very much in advance