「Flutter/外部ライブラリ/url launcher」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→メール起動サンプル) |
|||
| (同じ利用者による、間の19版が非表示) | |||
| 行1: | 行1: | ||
==url launcherとは== | ==url launcherとは== | ||
外部起動させるもので、メールやブラウザを起動できる。 | 外部起動させるもので、メールやブラウザを起動できる。 | ||
| + | |||
| + | ==公式== | ||
| + | https://pub.dev/packages/url_launcher | ||
==インストール== | ==インストール== | ||
pubspec.yaml | pubspec.yaml | ||
| − | url_launcher: | + | dependencies: |
| + | 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 { |
| − | + | 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を追加 | ||
| + | <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でビルド停止から起動するなどするとよい。
