「Unity/URLからアプリ起動/iOS」の版間の差分
提供: 初心者エンジニアの簡易メモ
| 行46: | 行46: | ||
content-type: application/json | content-type: application/json | ||
</pre> | </pre> | ||
| + | ===Unityアプリ側設定=== | ||
| + | xcodeの設定を自動で追加する。 | ||
| + | Assets/Editor/AddAssociatedDomains.cs | ||
| + | <pre> | ||
| + | using UnityEditor; | ||
| + | using UnityEditor.Callbacks; | ||
| + | using UnityEditor.iOS.Xcode; | ||
| + | using System.IO; | ||
| + | |||
| + | public class AddAssociatedDomains | ||
| + | { | ||
| + | [PostProcessBuild] | ||
| + | public static void OnPostProcessBuild(BuildTarget target, string path) | ||
| + | { | ||
| + | if (target != BuildTarget.iOS) return; | ||
| + | |||
| + | string projPath = PBXProject.GetPBXProjectPath(path); | ||
| + | PBXProject proj = new PBXProject(); | ||
| + | proj.ReadFromFile(projPath); | ||
| + | |||
| + | string targetGuid = proj.GetUnityMainTargetGuid(); | ||
| + | |||
| + | // 1. entitlements ファイルのパス | ||
| + | string entitlementsPath = Path.Combine(path, "Unity-iPhone.entitlements"); | ||
| + | |||
| + | // 2. entitlements がなければ作る | ||
| + | if (!File.Exists(entitlementsPath)) | ||
| + | { | ||
| + | var plist = new PlistDocument(); | ||
| + | plist.root.CreateArray("com.apple.developer.associated-domains"); | ||
| + | File.WriteAllText(entitlementsPath, plist.WriteToString()); | ||
| + | } | ||
| + | |||
| + | // 3. entitlements をプロジェクトに追加 | ||
| + | proj.AddFile(entitlementsPath, "Unity-iPhone.entitlements"); | ||
| + | proj.AddBuildProperty(targetGuid, "CODE_SIGN_ENTITLEMENTS", "Unity-iPhone.entitlements"); | ||
| + | |||
| + | // 4. entitlements に applinks を追加 | ||
| + | var ent = new PlistDocument(); | ||
| + | ent.ReadFromFile(entitlementsPath); | ||
| + | var array = ent.root["com.apple.developer.associated-domains"].AsArray(); | ||
| + | array.values.Clear(); | ||
| + | array.AddString("applinks:typing.nonip.net"); | ||
| + | File.WriteAllText(entitlementsPath, ent.WriteToString()); | ||
| + | |||
| + | // 5. Capability を追加(ここで entitlementsPath が必要) | ||
| + | proj.AddCapability(targetGuid, PBXCapabilityType.AssociatedDomains, "Unity-iPhone.entitlements", false); | ||
| + | |||
| + | proj.WriteToFile(projPath); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | iOSビルドすれば良い。 | ||
| + | |||
| + | ===動作=== | ||
iPhoneとかの、safariで、以下を開くと、アプリが起動する。 | iPhoneとかの、safariで、以下を開くと、アプリが起動する。 | ||
ttps://example.com/invite | ttps://example.com/invite | ||
2025年12月5日 (金) 04:27時点における版
目次
iOSの場合
独自ドメインで、公開場所に、apple-app-site-associationファイルを用意
例:
ttps://example.com/.well-known/apple-app-site-association
MIME が application/json にする。
apple-app-site-association ファイルの中身
{
"applinks": {
"details": [
{
"appIDs": ["TEAMID.bundleid"],
"paths": [ "/invite*" ]
}
]
}
}
appIDsの部分の例
TEAMID:8VA6Uxxxxx bundleid:com.example.app1 appIDs:8VA6Uxxxxx.com.example.app1
Content-Typeをapplication/jsonへ
.well-known/.htaccess を設置
<Files "apple-app-site-association">
ForceType application/json
Header set Content-Type "application/json"
</Files>
確認
$ curl -I ttps://example.com/.well-known/apple-app-site-association HTTP/2 200 server: nginx date: Thu, 04 Dec 2025 18:18:45 GMT content-type: application/json
Unityアプリ側設定
xcodeの設定を自動で追加する。
Assets/Editor/AddAssociatedDomains.cs
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
public class AddAssociatedDomains
{
[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget target, string path)
{
if (target != BuildTarget.iOS) return;
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromFile(projPath);
string targetGuid = proj.GetUnityMainTargetGuid();
// 1. entitlements ファイルのパス
string entitlementsPath = Path.Combine(path, "Unity-iPhone.entitlements");
// 2. entitlements がなければ作る
if (!File.Exists(entitlementsPath))
{
var plist = new PlistDocument();
plist.root.CreateArray("com.apple.developer.associated-domains");
File.WriteAllText(entitlementsPath, plist.WriteToString());
}
// 3. entitlements をプロジェクトに追加
proj.AddFile(entitlementsPath, "Unity-iPhone.entitlements");
proj.AddBuildProperty(targetGuid, "CODE_SIGN_ENTITLEMENTS", "Unity-iPhone.entitlements");
// 4. entitlements に applinks を追加
var ent = new PlistDocument();
ent.ReadFromFile(entitlementsPath);
var array = ent.root["com.apple.developer.associated-domains"].AsArray();
array.values.Clear();
array.AddString("applinks:typing.nonip.net");
File.WriteAllText(entitlementsPath, ent.WriteToString());
// 5. Capability を追加(ここで entitlementsPath が必要)
proj.AddCapability(targetGuid, PBXCapabilityType.AssociatedDomains, "Unity-iPhone.entitlements", false);
proj.WriteToFile(projPath);
}
}
iOSビルドすれば良い。
動作
iPhoneとかの、safariで、以下を開くと、アプリが起動する。 ttps://example.com/invite
