facebook twitter hatena line email

「Flutter/外部ライブラリ/url launcher」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ブラウザ起動サンプル)
 
(同じ利用者による、間の13版が非表示)
行8: 行8:
 
pubspec.yaml
 
pubspec.yaml
 
  dependencies:
 
  dependencies:
   url_launcher: 5.2.5
+
   url_launcher: ^5.4.1
 +
 
 +
==ブラウザ起動サンプル==
 +
<pre>
 +
_launchURL() async {
 +
  const url = 'https://flutter.dev';
 +
  if (await canLaunch(url)) {
 +
    await launch(url);
 +
  } else {
 +
    throw 'Could not launch $url';
 +
  }
 +
}
 +
</pre>
 +
参考:https://qiita.com/superman9387/items/868ce6ad60b3c177bff1
  
 
==メール起動サンプル==
 
==メール起動サンプル==
  String url = "mailto:sample@example.com?subject=subject1&body=" + Uri.encodeComponent("本文1");
+
  _launchMail() async {
if (await canLaunch(url)) {
+
    String url = "mailto:sample@example.com?subject=subject1&body=" + Uri.encodeComponent("本文1");
      await launch(url);
+
    if (await canLaunch(url)) {
} else {
+
          await launch(url);
      throw 'Could not launch $url';
+
    } else {
 +
          throw 'Could not launch $url';
 +
    }
 
  }
 
  }
  
 
参考:https://qiita.com/sekitaka_1214/items/c7b3251e3d4ea1dd4f53
 
参考:https://qiita.com/sekitaka_1214/items/c7b3251e3d4ea1dd4f53
  
==ブラウザ起動サンプル==
+
==url起動サンプル==
  _launchURL() async {
+
httpsのurlを開く場合
     const url = "http://https://www.google.co.jp/";
+
  import 'package:url_launcher/url_launcher_string.dart';
     if (await canLaunch(url)) {
+
  void launchAppPrivacyUrl() async {
       await launch(url);
+
     String url = "ttps://example.com/privacy.html";
 +
     if (await canLaunchUrlString(url)) {
 +
       await launchUrlString(url);
 
     } else {
 
     } else {
       throw 'Could not Launch $url';
+
       throw 'Could not launch $url';
 
     }
 
     }
 
   }
 
   }
参考:https://qiita.com/superman9387/items/868ce6ad60b3c177bff1
+
 
 +
↓はNG。(QUERY_ALL_PACKAGESをつけると、AndroidのPlayStoreで、審査が入り、理由を問われるが審査が通らなかった)
 +
 
 +
android/app/src/main/AndroidManifest.xmlにuser-permissionのQUERY_ALL_PACKAGESを追加
 +
<pre>
 +
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
 +
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
 +
    <queries>
 +
        <package android:name="com.example.app1" />
 +
        <intent>
 +
            <action android:name="android.intent.action.VIEW" />
 +
            <category android:name="android.intent.category.BROWSABLE" />
 +
            <data android:scheme="https" />
 +
        </intent>
 +
</pre>
 +
 
 +
==component name for is nullが起こる場合==
 +
エラー詳細
 +
I/UrlLauncher(19372): component name for ttps://example.com/privacy.html is null
 +
 
 +
対策方法
 +
 
 +
android/app/src/main/AndroidManiest.xml
 +
<pre>
 +
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
 +
    <uses-permission android:name="android.permission.INTERNET"/>
 +
    <queries>
 +
        <package android:name="com.mskjgo.memotyou" />
 +
        <intent>
 +
            <action android:name="android.intent.action.VIEW" />
 +
            <category android:name="android.intent.category.BROWSABLE" />
 +
            <data android:scheme="https" />
 +
        </intent>
 +
            <intent>
 +
                <action android:name="android.intent.action.VIEW" />
 +
                <data android:scheme="https" />
 +
            </intent>
 +
    </queries>
 +
</pre>
 +
参考:https://zenn.dev/flutteruniv_dev/articles/ee377ae7f1fe05
 +
 
 +
==ui_dart_state.cc(157)が起こる場合==
 +
エラー詳細
 +
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method canLaunch on channel plugins.flutter.io/url_launcher)
 +
 
 +
おそらく再ビルドしてないためで、以下実行か、
 +
$ flutter clean
 +
 
 +
androidStudioでビルド停止から起動するなどするとよい。
 +
 
 +
https://github.com/flutter/flutter/issues/10967

2024年1月3日 (水) 01:10時点における最新版

url launcherとは

外部起動させるもので、メールやブラウザを起動できる。

公式

https://pub.dev/packages/url_launcher

インストール

pubspec.yaml

dependencies:
  url_launcher: ^5.4.1

ブラウザ起動サンプル

_launchURL() async {
  const url = 'https://flutter.dev';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

参考:https://qiita.com/superman9387/items/868ce6ad60b3c177bff1

メール起動サンプル

_launchMail() async {
    String url = "mailto:sample@example.com?subject=subject1&body=" + Uri.encodeComponent("本文1");
    if (await canLaunch(url)) {
         await launch(url);
    } else {
         throw 'Could not launch $url';
    }
}

参考:https://qiita.com/sekitaka_1214/items/c7b3251e3d4ea1dd4f53

url起動サンプル

httpsのurlを開く場合

import 'package:url_launcher/url_launcher_string.dart';
 void launchAppPrivacyUrl() async {
   String url = "ttps://example.com/privacy.html";
   if (await canLaunchUrlString(url)) {
     await launchUrlString(url);
   } else {
     throw 'Could not launch $url';
   }
 }

↓はNG。(QUERY_ALL_PACKAGESをつけると、AndroidのPlayStoreで、審査が入り、理由を問われるが審査が通らなかった)

android/app/src/main/AndroidManifest.xmlにuser-permissionのQUERY_ALL_PACKAGESを追加

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
    <queries>
        <package android:name="com.example.app1" />
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="https" />
        </intent>

component name for is nullが起こる場合

エラー詳細

I/UrlLauncher(19372): component name for ttps://example.com/privacy.html is null

対策方法

android/app/src/main/AndroidManiest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET"/>
    <queries>
        <package android:name="com.mskjgo.memotyou" />
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="https" />
        </intent>
            <intent>
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="https" />
            </intent>
    </queries>

参考:https://zenn.dev/flutteruniv_dev/articles/ee377ae7f1fe05

ui_dart_state.cc(157)が起こる場合

エラー詳細

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method canLaunch on channel plugins.flutter.io/url_launcher)

おそらく再ビルドしてないためで、以下実行か、

$ flutter clean

androidStudioでビルド停止から起動するなどするとよい。

https://github.com/flutter/flutter/issues/10967