7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【iOS】Universal Links(別アプリから起動)の実装方法とサーバーレスでの実装方法(Firebase Hosting)

Posted at

どうも、日焼け対策をしておけばよかったと後悔しているiOSエンジニアのkaです。

サーバーを用意せずとも無料でFirebase Hostingを使ってUniversal Linksの実装をできますよということで、自分でやってみてハマりポイントもいくつかあったので紹介します。

#Universal Linksの実装
###Apple Developerサイトでの準備

  1. Certificates, Identifiers & Profilesのページを開く
  • Identifiersの一覧ページで設定したいIdentifierを選択
    Associated Domainsのチェックを選択してSave
    スクリーンショット 2020-08-25 10.46.27.png
  • provisioning profileが取り消されているので、Profilesの一覧ページで再度選択してEdit
  • 再度保存してDownload
  • これでAssociated Domainsが有効になったProvisioning Profileが作成できました。

###Xcodeでの実装

  1. Projectファイルを開く
  2. メニュー → Editor → Add Capability Associated Domainsを選択
  3. ドメインを入れる applinks:の後にドメインを入れます httpsの文字列は入りません(例:applinks:sampledomain.co.jp)
    スクリーンショット 2020-08-25 11.02.20.png

###AppDelegateの実装
外部アプリでドメインをタップしてアプリが起動した際にはAppDelegateでハンドリングされます。
AppDelegateに以下のコードを挿入します。

AppDelegate.swift
func application(_ application: UIApplication,
                     continue userActivity: NSUserActivity,
                     restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let incomingURL = userActivity.webpageURL,
        let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
            return false
    }
    return true
}

#Firebase Hostingの設定
###apple-app-site-associationファイルの準備
apple-app-site-associationというファイル名で.jsonなどの拡張子は要りません

{
  "applinks": {
       "apps": [],
        "details": [
           {
               "appID":"{Team ID}.{bundle ID}",
               "paths":[ "*" ]
           }
         ]
    }
}

TeamIDはDeveloperAccountサイトで確認できるTeam IDです。
pathsの部分はドメインの全てのpathをアプリに通すという意味のワイルドカードに設定しています。
特定のpathはアプリに遷移させないという設定をすることもこの部分でできます。

###配置
ドメイン/.well-known/apple-app-site-association
でアクセスできる場所に配置します。

ここで注意点ですが、
最初自分が試したのはドメインの直下に
ドメイン/apple-app-site-association
のように配置していたのですが何故かうまくいきませんでした。

apple-app-site-associationはアプリのインストール時にアクセスして取得しにいくのですが
.well-known配下のファイルを優先してアクセスしにいくという仕様があるようで
何とドメイン/.well-known/apple-app-site-associationにアクセスしてみたところ空のapple-app-site-associationが最初から配置されていたのです。
そこで、.well-known配下に配置したところうまくいきました。

###firebase.jsonの設定
firebase.jsonを編集して
apple-app-site-associationのContentTypeをapplication/jsonになるようにします。

以上の実装でメモ帳アプリなどでドメインを書いてタップすればスムーズにアプリ起動ができるはずです。

#補足

  • 運用しているアプリでapple-app-site-associationを変更する場合アプリのインストールとアップデート時にしか更新されないので注意が必要
  • 同一のドメインから同一のドメインへリンクしているとアプリ起動しない。Safariの場合は上部にアプリを開くボタンがでできてそこからアプリを開くことができる。

公式ドキュメント:
Universal Links
Firebase Hosting

#終わりです

プールや海に遊びにいくときは絶対にラッシュガードを着て遊びましょう!

Brewus,Inc.
株式会社ブリューアス
https://brewus.co.jp

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?