如何从 Flutter 应用程序打开应用程序? [英] How to open an application from a Flutter app?

查看:45
本文介绍了如何从 Flutter 应用程序打开应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Flutter 应用程序,我需要在其中向用户显示某个地点的导航.那么,如何像在 Android 中使用外部 Intent 那样从 Flutter 应用程序打开地图应用程序?

I am developing a Flutter app, in which I need to show navigation to the user for a place. So, how can I open a map application from my Flutter app like we do using external intent in Android?

或者他们有任何 flutter-plugin 吗?

Or their is any flutter-plugin for it?

推荐答案

我建议你使用 url_launcher dart 包.

I suggest you to use url_launcher dart package.

通过这种方式,您可以使用所有 url 模式打开(phonesms,甚至是 maps,就像您的情况一样).

In this way you can use all url schemas to open (phone, sms, and even maps as in your case).

为了在 Android 和 iOS 中打开 Google 地图,您可以使用 通用 Android 地图Hemanth Raj 建议的 URI 架构.

In order to open Google Maps either in Android and iOS you could use the general Android Maps URI schema as suggested by Hemanth Raj.

_openMap() async {
    const url = 'https://www.google.com/maps/search/?api=1&query=52.32,4.917';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

如果您想在 Android 上做出选择,您可以使用一般的 geo: URI 架构.

If you want to give a choice on Android you could use the general geo: URI schema.

如果您想专门打开 iOS Maps API,您可以使用 库比蒂诺地图 URI 架构.

If you want specifically open iOS Maps API you can use Cupertino Maps URI schema.

如果您选择区分 Android 和 iOS(不为所有平台使用 Google Maps Api 架构),您也必须在打开地图调用中以如下方式执行此操作:

If you choose to distinguish between Android and iOS (not using Google Maps Api schema for all platform) you have to do it also in your open map call in a way like this:

_openMap() async {
    // Android
    const url = 'geo:52.32,4.917';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      // iOS
      const url = 'http://maps.apple.com/?ll=52.32,4.917';
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }
  }

或者您可以在运行时使用 <检查操作系统code>dart.io 库 Platform:

Or you can check the OS at runtime with dart.io library Platform class:

import 'dart:io';

_openMap() async {
    // Android
    var url = 'geo:52.32,4.917';
    if (Platform.isIOS) {
      // iOS
      url = 'http://maps.apple.com/?ll=52.32,4.917';
    }
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

现在我完成了软管维护(真正的......不是一些代码重构......^^')我可以完成我的答案.

Now that I finished hosekeeping (the real one... not some code refactoring... ^^') I can finish my answer.

正如我在开始使用 url_launcher 时告诉您的那样,您可以使用所有 URI 模式来调用、发送短信、发送电子邮件等.

As I tell you at the beginning with url_launcher you can use all URI Schemas in order to call, send sms, send e-mail etc.

这里有一些代码可以做到这一点:

Here some code to do that:

_sendMail() async {
    // Android and iOS
    const uri = 'mailto:test@example.org?subject=Greetings&body=Hello%20World';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
    throw 'Could not launch $uri';
    }
  }

  _callMe() async {
    // Android
    const uri = 'tel:+1 222 060 888';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
      // iOS
      const uri = 'tel:001-22-060-888';
      if (await canLaunch(uri)) {
        await launch(uri);
      } else {
        throw 'Could not launch $uri';
      }
    }
  }

  _textMe() async {
    // Android
    const uri = 'sms:+39 349 060 888';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
      // iOS
      const uri = 'sms:0039-222-060-888';
      if (await canLaunch(uri)) {
        await launch(uri);
      } else {
        throw 'Could not launch $uri';
      }
    }
  }

即使 URI 架构应该是标准 (RFC),有时权威path 部分可能因框架(Android 或 iOS)而异.

Even if URI schema should be standards (RFC) sometimes the authority and path parts of them could differ between frameworks (Android or iOS).

所以在这里我管理不同的操作系统,但是,你可以用 dart.ioPlatform:

So here I manage the different OSes with exception but, you could do it better with dart.io library Platform class:

import 'dart:io'

然后在代码中:

if (Platform.isAndroid) {

} else if (Platform.isIOS) {

}

我建议您始终在两种环境中测试它们.

I suggest you always test them in both environments.

您可以在此处查看 Android 和 iOS 架构文档:

You can check the Android and iOS schema documenation here:

如果你想要类似于 Android 中的 startActivity(但仅适用于 Android 平台),你可以使用 dart 包 android_intent.

If you wanna something similar to startActivity in Android (but that works only for Android platform) you can use the dart package android_intent.

这篇关于如何从 Flutter 应用程序打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆