【Flutter】url_launcherの使い方

Flutter

url_launcherとは?

url_launcherは、FlutterアプリケーションでURLを起動するためのプラグイン。
アプリ内からブラウザを開いてウェブページを表示したり、電話アプリを起動して通話を開始したり、メールアプリを開いてメールを作成したりすることが簡単にできる。

url_launcher | Flutter package
Flutter plugin for launching a URL. Supports web, phone, SMS, and email schemes.

url_launcherの使い方

インストール

url_launcher install | Flutter package
Flutter plugin for launching a URL. Supports web, phone, SMS, and email schemes.

iOS

Info.plistファイルに以下を追加する:

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>tel</string>
  <string>mailto</string>
  <string>https</string>
  <string>http</string>
</array>

Android

AndroidManifest.xmlファイルに以下を追加する:

<queries>
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="http" />
  </intent>
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
  <intent>
    <action android:name="android.intent.action.DIAL" />
    <data android:scheme="tel" />
  </intent>
  <intent>
    <action android:name="android.intent.action.SENDTO" />
    <data android:scheme="mailto" />
  </intent>
</queries>

使用方法

canLaunch

指定したURLが起動可能かどうかを確認する関数。起動可能な場合はtrueを、そうでない場合はfalseを返す。

使い方
bool canOpen = await canLaunch('https://www.example.com');

launch

指定したURLを実際に起動する関数。例えば、ブラウザでウェブページを開いたり、電話アプリを起動して通話を開始したりする。

使い方
await launch('https://www.example.com');

launchUrl

より詳細な制御が可能なURL起動関数。例えば、ブラウザの新しいタブで開くか、既存のタブで開くかなどのオプションを指定できる。

使い方
await launchUrl('https://www.example.com', webOnlyWindowName: '_blank');

よく使うURLスキーム

  • http: ウェブページを開く
  • https: セキュアなウェブページを開く
  • tel: 電話をかける
  • sms: SMSを送る
  • mailto: メールを送る
タイトルとURLをコピーしました