🔨

Xcode 13以上でInfo.plistを安全かつ簡単に作成する

2023/05/05に公開

Xcode 13 以降ではデフォルトで Info.plist が生成されない

Xcode 13 以降では Release Notes の通りで、プロジェクト設定の中(Info タブ)でプロパティリストが内包されるようになったため、 Info.plist ファイルはデフォルトでは作成されなくなりました。

Projects created from several templates no longer require configuration files such as entitlements and Info.plist files. Configure common fields in the target’s Info tab, and build settings in the project editor. These files are added to the project when additional fields are used. (68254857)

しかし、直接 Source Code として開いて編集できるなど Info.plist を使うメリットはまだ残っていますし、 Xcode 自体が作成してくれる場合もあります(Xcode 14現在)。

自前で Property List を作成して設定を紐づけてもよいのですが、いくつか手順があって煩雑だったりしますし、間違えていて実は適用されていないみたいなリスクもあります。

そこで、今回は上述した通り Xcode 自体が自動で生成してくれる機能を利用して、 Info.plist ファイルを安全かつ簡単に作成する手順を示します。

Info.plist 作成手順

  1. まずは、Project Navigator のルートを選択し、xcodeproj ファイルのプロジェクト設定画面を開きます

open xcodeproj

  1. つづいて Info タブを開いて、Document Types を展開して、 + ボタンをタップします

info tab

この時点で差分を確認すると、 Info.plist がプロジェクトに生成され、必要な設定なども自動で適用されていることがわかります(便利)

diff --git a/InfoPlistTest.xcodeproj/project.pbxproj b/InfoPlistTest.xcodeproj/project.pbxproj
index e4c1395..f194918 100644
--- a/InfoPlistTest.xcodeproj/project.pbxproj
+++ b/InfoPlistTest.xcodeproj/project.pbxproj
@@ -20,6 +20,7 @@
                0136389E2A05003600149098 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
                013638A02A05003600149098 /* InfoPlistTest.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = InfoPlistTest.entitlements; sourceTree = "<group>"; };
                013638A22A05003600149098 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
+               013638A92A0500B100149098 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
 /* End PBXFileReference section */

 /* Begin PBXFrameworksBuildPhase section */
@@ -52,6 +53,7 @@
                013638992A05003400149098 /* InfoPlistTest */ = {
                        isa = PBXGroup;
                        children = (
+                               013638A92A0500B100149098 /* Info.plist */,
                                0136389A2A05003400149098 /* InfoPlistTestApp.swift */,
                                0136389C2A05003400149098 /* ContentView.swift */,
                                0136389E2A05003600149098 /* Assets.xcassets */,
@@ -269,6 +271,7 @@
                                ENABLE_HARDENED_RUNTIME = YES;
                                ENABLE_PREVIEWS = YES;
                                GENERATE_INFOPLIST_FILE = YES;
+                               INFOPLIST_FILE = InfoPlistTest/Info.plist;
                                "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
                                "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
                                "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
@@ -307,6 +310,7 @@
                                ENABLE_HARDENED_RUNTIME = YES;
                                ENABLE_PREVIEWS = YES;
                                GENERATE_INFOPLIST_FILE = YES;
+                               INFOPLIST_FILE = InfoPlistTest/Info.plist;
                                "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
                                "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
                                "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;

diff --git a/InfoPlistTest/Info.plist b/InfoPlistTest/Info.plist
new file mode 100644
index 0000000..ef6228b
--- /dev/null
+++ b/InfoPlistTest/Info.plist
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+       <key>CFBundleDocumentTypes</key>
+       <array>
+               <dict/>
+       </array>
+</dict>
+</plist>
  1. 最後にダミーの Document Types は必要ないので、 作成された Info.plist 上から消しておきます

info plist

diff --git a/InfoPlistTest/Info.plist b/InfoPlistTest/Info.plist
index ef6228b..0c67376 100644
--- a/InfoPlistTest/Info.plist
+++ b/InfoPlistTest/Info.plist
@@ -1,10 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
-<dict>
-       <key>CFBundleDocumentTypes</key>
-       <array>
-               <dict/>
-       </array>
-</dict>
+<dict/>
 </plist>

以上で設定完了です! お疲れ様でした 🎉

Discussion