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?

Flutterで別アプリ起動する手順(カスタムURLスキームだけ)

Posted at

想定している要件

  • カスタムURLスキーム(CustomURLScheme)でインストール済みの別アプリへ遷移させたい
  • Androidのアプリリンク(App Links)は使用しない
  • iOSのユニバーサルリンク(Universal Links)は使用しない
  • そのほかFirebaseDynamicLinksなども使用しない
  • ユーザーは必ず遷移先のアプリをインストール済みである

遷移先のアプリの事前確認事項

Android

  • <!-- 追加する部分 -->が存在するか確認し、ない場合は追加、ある場合はandroid:schemeの値を確認する
  • android:exported="true"になっているか確認する
    • 記述がない場合はfalse扱いとなるためtrueを明示的に記述する
  • yourcustomschemeは衝突しないようにユニークな名前を付ける
android/app/src/main/AndroidManifest.xml
<activity
    android:name=".MainActivity"
    android:exported="true"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize">

    <!-- 追加する部分 -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="yourcustomscheme"
            android:host="yourhost" />
    </intent-filter>

</activity>

iOS

  • 下記のCFBundleURLTypesが存在するか確認し、ない場合は追加、ある場合はCFBundleURLSchemesの値を確認する
  • yourcustomschemeは衝突しないようにユニークな名前を付ける
info.plist
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string>com.yourcompany.yourapp</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>yourcustomscheme</string>
    </array>
  </dict>
</array>

遷移元のアプリの設定事項

パッケージのインストール

pubspeck.yml
dependencies:
  url_launcher: ^6.3.0

Flutterのコード

import 'dart:io' show Platform;
import 'package:url_launcher/url_launcher.dart';

void _openYourApp() async {
    const androidUrl = 'yourcustomscheme://'; // YouTubeなら'youtube://'
    const iosUrl ='yourcustomscheme://'; //  YouTubeなら'vnd.youtube://'
    
    final targetUrlStr = Platform.isAndroid ? androidUrl : iosUrl;
    final targetUrl = Uri.parse(targetUrlStr);

    if (await canLaunchUrl(targetUrl)) { // インストール済みでAppを開けるか判定
        await launchUrl(url);
    } else {
      print('yourAppを開くことができませんでした。');
    }
  }
}

遷移先のスキーマが分からない場合

有名なものや、標準アプリに関しては調べればわかりますが(instagram,twitter等)、個人が開発したいアプリに遷移をさせたい場合は、開発者に問い合わせると早いかと思います。

参考リンク

https://pub.dev/packages/url_launcher
https://note.com/toridori_inc/n/nb7237a217a51
https://developer.android.com/privacy-and-security/risks/android-exported?hl=ja#overview
https://zenn.dev/quicksilversel/articles/7062da63e549fd

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?