如何从Flutter应用程序打开应用程序?

Gha*_*iya 12 flutter

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

或者他们是任何扑动插件吗?

sha*_*eep 38

我建议你使用url_launcher dart包.

通过这种方式,您可以使用所有URL模式打开(phone,sms甚至maps在您的情况下).

要在Android和iOS中打开Goog​​le地图,您可以使用Hemanth Raj建议的常规Android地图URI架构.

_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';
    }
  }
Run Code Online (Sandbox Code Playgroud)

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

如果您想要专门打开的iOS Maps API,可以使用Cupertino Maps URI架构.

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

_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';
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

或者您可以在运行时使用dart.ioPlatform检查操作系统:

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';
    }
  }
Run Code Online (Sandbox Code Playgroud)

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

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

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

_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';
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

即使URI模式应该是标准(RFC)有时authoritypath他们的部分可能的框架(Android或iOS)之间的差异.

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

import 'dart:io'
Run Code Online (Sandbox Code Playgroud)

然后在代码中:

if (Platform.isAndroid) {

} else if (Platform.isIOS) {

}
Run Code Online (Sandbox Code Playgroud)

我建议你总是在两种环境中测试它们.

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

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

  • 哇...这是一个很好的答案。和 +1 为家政部分:) (2认同)

cos*_*nus 15

对于 iOS 使用,不涉及浏览器,直接到应用程序:

//waze 
canLaunch("waze://") 
launch("waze://?ll=${latitude},${longitude}&navigate=yes"); 
//gmaps 
canLaunch("comgooglemaps://") 
launch("comgooglemaps://?saddr=${latitude},${longitude}&directionsmode=driving")
Run Code Online (Sandbox Code Playgroud)

你需要做的是添加到Info.plist:

<key>LSApplicationQueriesSchemes</key>  
<array>         
  <string>comgooglemaps</string>        
  <string>waze</string>     
</array>
Run Code Online (Sandbox Code Playgroud)