Flutter检测外部应用安装状态实现

在Flutter中如何检测设备上是否安装了某个外部应用?目前需要在Android和iOS平台上实现这个功能,但找不到统一的解决方案。尝试过使用url_launcherandroid_intent插件,但只能打开应用而无法检测安装状态。iOS上似乎还需要配置特定的URL schemes,但不太清楚具体如何操作。有没有跨平台的方案或推荐的插件可以准确判断目标应用是否已安装?如果必须分平台处理,能否提供完整的代码示例?

3 回复

要检测外部应用是否已安装,可以使用 Flutter 的 package_info_plusuniversal_html 插件。首先添加依赖:

dependencies:
  package_info_plus: ^2.0.0
  universal_html: ^2.0.0

然后编写代码:

import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:universal_html/html.dart' as html;

Future<bool> isAppInstalled(String packageName) async {
  PackageInfo packageInfo = await PackageInfo.fromPlatform();
  String platform = html.window.navigator.platform.toLowerCase();

  if (platform.contains('android')) {
    return html.window.navigator.userAgent.contains(packageName);
  } else if (platform.contains('ios')) {
    // iOS需通过URL Scheme或App Link检查
    final uri = Uri(scheme: 'your_scheme', host: packageName);
    try {
      return await launchUrl(uri, mode: LaunchMode.externalApplication);
    } catch (_) {
      return false;
    }
  }
  return false;
}

void main() {
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isInstalled = false;

  Future<void> checkInstallStatus() async {
    bool status = await isAppInstalled('com.example.targetapp');
    setState(() {
      _isInstalled = status;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("App Install Check")),
      body: Center(
        child: ElevatedButton(
          onPressed: checkInstallStatus,
          child: Text(_isInstalled ? "已安装" : "未安装"),
        ),
      ),
    );
  }
}

这段代码会根据平台判断目标应用是否安装,并显示对应状态。

更多关于Flutter检测外部应用安装状态实现的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中检测外部应用是否已安装,可以通过平台通道调用原生代码实现。以下是实现步骤:

  1. 创建一个Flutter项目。
  2. pubspec.yaml文件中添加url_launcher插件(可选)。
  3. lib/main.dart中创建平台通道。
import 'package:flutter/services.dart';

class AppChecker {
  static const platform = MethodChannel('appchecker');

  static Future<bool> isAppInstalled(String packageName) async {
    try {
      final bool result = await platform.invokeMethod('isAppInstalled', {'packageName': packageName});
      return result;
    } on PlatformException catch (e) {
      print("Error: ${e.message}");
      return false;
    }
  }
}
  1. 在Android端(android/app/src/main/java),实现检测逻辑:
public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "appchecker";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new MethodChannel(getFlutterEngine()..getDartExecutor(), CHANNEL)
            .setMethodCallHandler((call, result) -> {
                if (call.method.equals("isAppInstalled")) {
                    String packageName = call.argument("packageName");
                    boolean installed = isPackageInstalled(packageName);
                    result.success(installed);
                } else {
                    result.notImplemented();
                }
            });
    }

    private boolean isPackageInstalled(String packageName) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
  1. 调用方法:AppChecker.isAppInstalled('com.example.app')

注意权限声明和测试设备的配置。

在 Flutter 中检测外部应用是否安装,可以使用 url_launcherandroid_intent(针对 Android)和 ios_application_identifier(针对 iOS)来实现。以下是实现方法:

1. 使用 url_launcher 检测(跨平台)

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

Future<bool> isAppInstalled(String packageName) async {
  try {
    // 尝试打开应用
    final uri = Uri.parse(packageName.contains(':') ? packageName : '$packageName:');
    return await canLaunchUrl(uri);
  } catch (e) {
    return false;
  }
}

// 使用示例
bool installed = await isAppInstalled('com.example.app'); 
// 或使用 URL scheme: 'exampleapp://'

2. 平台特定方法

Android:

# 在 pubspec.yaml 中添加
dependencies:
  android_intent: ^2.0.0
import 'package:android_intent/android_intent.dart';

Future<bool> isAndroidAppInstalled(String packageName) async {
  final intent = AndroidIntent(
    action: 'action_view',
    package: packageName,
  );
  try {
    await intent.launch();
    return true;
  } catch (e) {
    return false;
  }
}

iOS:

# 在 pubspec.yaml 中添加
dependencies:
  ios_application_identifier: ^0.0.3
import 'package:ios_application_identifier/ios_application_identifier.dart';

Future<bool> isIOSAppInstalled(String bundleId) async {
  try {
    return await IosApplicationIdentifier.isAppInstalled(bundleId);
  } catch (e) {
    return false;
  }
}

注意事项

  1. 需要分别处理 Android 和 iOS 的包名/URL scheme
  2. 在 Android 上需要在 AndroidManifest.xml 添加查询权限
  3. iOS 需要预先知道目标应用的 URL scheme 或 bundle identifier

建议优先使用 url_launcher 的跨平台方案,除非需要更精确的控制。

回到顶部