[iOS] Universal Linksの「apple-app-site-association」へのアクセスにIP制限がかかっている場合の動作について

Universal Linksの「apple-app-site-association」へのアクセスにIP制限がかかっている場合の動作について調べてみましたので紹介します。
2021.12.15

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんにちは。モバイル事業部の平屋です。

Universal Linksの「apple-app-site-association」へのアクセスにIP制限がかかっている場合の動作について調べてみましたので紹介します。

apple-app-site-association

「apple-app-site-association」はUniversal Linksの動作に必要なファイルで、サーバーに配置され、iOS端末から参照されます。

検証環境

今回試した環境は以下の通りで、「apple-app-site-association」は特定のIPからしかアクセスできないようになってます。

  • アプリのサポートOS: iOS 13以降
  • 「apple-app-site-association」が配置されたサーバーの制限: IP制限あり

iOS 14以降の場合、iOS端末はAppleが管理するCDNから「apple-app-site-association」を取得します。

In macOS 11 and later and iOS 14 and later, apps request apple-app-site-association files from an Apple-managed content delivery network (CDN) specifically for associated domains, instead of directly from your web server. If the CDN has an old version of the file, or doesn’t already have a copy of the file, it connects to your web server to obtain the latest version. https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_associated-domains

オリジナルの「apple-app-site-association」はAppleのCDNが収集するため、IP制限がかかっていると、ファイルを取得できず、Universal Linksが機能しなくなってしまいます。

developerモードをONにして、CDNをバイパスする

「developerモード」をONにすると、AppleのCDNをバイパスできます。その名の通り、開発版のアプリ向けに用意されているもので、IPが許可された環境からアクセスすればUniversal Linksは機能します。

「developerモード」をONにするには、まず、Entitlementsファイルに記載するUniversal Linksのドメインに「developerモード」を指定(ドメイン末尾に?mode=developerを追加)します。

<dict>
	...

	<key>com.apple.developer.associated-domains</key>
	<array>
        // ドメイン末尾に`?mode=developer`を追加
		<string>applinks:example.com?mode=developer</string> 
		<string>
	</array>
</dict>
</plist>

また、以下を満たすようにします。

  • アプリを開発用プロファイルで署名する
    • 配布用プロファイルで署名されたアプリはNG
  • 使用するデバイスでdeveloper modeをONにする
    • 設定アプリ > Developer > Associated Domains Development をONにする
    • 使用できる端末は開発用のものに限られる

As an additional precaution, only apps that you sign with a development profile can use developer mode, and users must opt-in on any device they use. https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_associated-domains

1点注意すべき点があって、「developerモード」をONにするために、Entitlementsファイルで「developerモード」を指定すると、iOS 13未満の端末でUniversal Linksが機能しなくなるようでした。(「developerモード」未対応だから?)

developerモードをONにできない場合

「apple-app-site-association」へのアクセスにIP制限がかかっていて、developerモードを使用できない場合(配布版アプリで動作確認したい場合など)にUniversal Linksを機能させるには以下の選択肢しかなさそうです。

  • 「apple-app-site-association」へのIP制限を解除する
  • IP制限のかかっていない別の環境を使う

まとめ

「apple-app-site-association」へのアクセスにIP制限がかかっている場合のUniversal Linksの動作をまとめると以下の内容になります。

iOS 14以降

  • developerモードON: Universal Linksが機能する
  • developerモードOFF: Universal Linksが機能しない

iOS 14未満

  • developerモードON: Universal Linksが機能しない
  • developerモードOFF: Universal Linksが機能する

developerモードをONにするための条件

  1. Entitlementsファイルに記載するUniversal Linksのドメインに「developerモード」を指定する
  2. アプリを開発用プロファイルで署名する
  3. 使用するデバイスでdeveloper modeをONにする

参考資料