0
0

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.

urlからExpoアプリを起動する

Posted at

やりたいこと

  • モバイルで、ブラウザからurlリンクで、インストール済みのExpoアプリを起動する
  • Deeplinkを使用しない。という例をベースにする

前提

  • Expo 4.7.2

参考資料

Linking
https://docs.expo.dev/guides/linking/

実装例

  • 実装例は、動作するクラスではなく、機能の実現に必要なコードのみを記載。

urlリンクを生成する処理

任意の画面等で.js
    const initialUrl = await Linking.getInitialURL();
       console.log(initialUrl+"?docid=zzkiRCXRlQa1Ukv88gva")
  • 開発環境だと次のような値が入る。 exp://127.0.0.1:19000
  • スタンドアロンアプリとして出力したあとはアプリのスキーマとなる。 スキーマは(myapp:// の部分) app.jsonで定義しておく必要がある
  • 参照: https://docs.expo.dev/guides/linking/#linking--module
  • アプリ内での取り回しのため、クエリパラメータで値を付与しておく。たとえばFirebaseで検索をかけるためのdocidを付与する

リンクからのアクセスを受け付ける処理

app.js
import React, {useEffect} from 'react';
import React, {useEffect} from 'react'; 
import * as Linking from 'expo-linking';

  useEffect(() => {
    // URLからの遷移
     urlFlg = false

    // リスナーの設定。UserEffectを利用すると設置とリムーブを次のように定義できる。
    Linking.addEventListener("url", _handleRedirect);
    return () => {
      Linking.removeEventListener("url", _handleRedirect);
    };
  });

// urlからのアクセスを処理する
handleRedirect =async (event) => {
    urlFlg = true; // useEffect自体はリンクから以外も実行されるため、Deeplink以外の場合と切り分けのためのフラグ

    // urlをパースし、引数を取得する
    let data = await Linking.parse(event.url)
      console.log("パラメータ===========")
      console.log(data.queryParams) //これで取得可能
      // たとえばクエリパラメータにdocidを仕込んでいた場合は次で取得する
      // 対象docid data.queryParams.docid

      // 画面遷移
      navigation.navigate('表示する画面',{id: data.queryParams.docid}); // たとえば画面遷移先にidを渡し、遷移後にそれで適切なデータを画面に表示する
  };

  if(!urlFlg){
    console.log("URLからの遷移では無い")
    // URLからの遷移ではない場合のナビゲーションを定義しておく
  }


0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?