如何在 flutter 中获取 bundle id

How to get bundle id in flutter

我使用以下方法获取应用名称和包名称,但我需要 iPhone 用户的 Bundle id。 我想分享一个应用 link。 我在 android 做了,但在 iPhone,我需要包 ID。

 Future<Null> _initPackageInfo() async {
        final PackageInfo info = await PackageInfo.fromPlatform();
        setState(() {
          _packageInfo = info;
          packageName = info.packageName;
          appName = info.appName;
          buildNumber = info.buildNumber;
        });
      }

要手动查找项目名称,您可以在 AndroidManifest.xml 或 Info.plist 中查找。

Android

在 Android 中,包名称在 AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    package="com.example.appname">

iOS

在iOS中,包名称是Info.plist中的包标识符:

<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>

Runner.xcodeproj/project.pbxproj:

中找到
PRODUCT_BUNDLE_IDENTIFIER = com.example.appname;

另见

在 Flutter 项目的 iOS 部分 Product Bundle Identifierproject.pbxproj 路径中的文件中:

[your-flutter-project-dir]\ios\Runner.xcodeproj\project.pbxproj

具体如下:

PRODUCT_BUNDLE_IDENTIFIER = com.app.flutter.example;

请注意,此值与 Flutter 项目中的 Android Package Name 相同。

您可以使用get_version 包获取iOS和Android上的App ID、版本名称和版本代码。

将此依赖项添加到您的应用程序中并像这样获取应用程序 ID

String projectAppID;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
  projectAppID = await GetVersion.appID;
} on PlatformException {
  projectAppID = 'Failed to get app ID.';
}

完整示例

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_version/get_version.dart';

void main() => runApp(new MyApp());

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _projectVersion = '';
  String _projectCode = '';
  String _projectAppID = '';
  String _projectName = '';

  @override
  initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await GetVersion.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    String projectVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectVersion = await GetVersion.projectVersion;
    } on PlatformException {
      projectVersion = 'Failed to get project version.';
    }

    String projectCode;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectCode = await GetVersion.projectCode;
    } on PlatformException {
      projectCode = 'Failed to get build number.';
    }

    String projectAppID;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectAppID = await GetVersion.appID;
    } on PlatformException {
      projectAppID = 'Failed to get app ID.';
    }
    
    String projectName;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectName = await GetVersion.appName;
    } on PlatformException {
      projectName = 'Failed to get app name.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
      _projectVersion = projectVersion;
      _projectCode = projectCode;
      _projectAppID = projectAppID;
      _projectName = projectName;
    });
  }

  

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Plugin example app'),
        ),
        body: new SingleChildScrollView(
          child: new ListBody(
            children: <Widget>[
              new Container(
                height: 10.0,
              ),
              new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('Name'),
                subtitle: new Text(_projectName),
              ),
              new Container(
                height: 10.0,
              ),
              new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('Running on'),
                subtitle: new Text(_platformVersion),
              ),
              new Divider(
                height: 20.0,
              ),
               new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('Version Name'),
                subtitle: new Text(_projectVersion),
              ),
              new Divider(
                height: 20.0,
              ),
              new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('Version Code'),
                subtitle: new Text(_projectCode),
              ),
              new Divider(
                height: 20.0,
              ),
              new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('App ID'),
                subtitle: new Text(_projectAppID),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

使用 get_version 包。这是最简单的方法

正在安装:

 dependencies:
   get_version: any

用法:

String projectAppID;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
  projectAppID = await GetVersion.appID;
} on PlatformException {
  projectAppID = 'Failed to get app ID.';
}

You can use it as String inside anything you want like Text widget etc ...


get_version的另一个小应用程序摘录:

import 'package:get_version/get_version.dart';    
  class _MyAppState extends State<MyApp> {
  String _projectAppID = '';
  @override
  initState() {
    super.initState();
    initPlatformState();
  }    
  // Platform messages are asynchronous, so we initialize in an async method.
  initPlatformState() async {
    String projectAppID;
    try {
      projectAppID = await GetVersion.appID;
    } catch (e) {
      projectAppID = 'Failed to get app ID.';
    }
    setState(() {
      _projectAppID = projectAppID;
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: ListTile(
          leading: new Icon(Icons.info),
          title: const Text('App ID'),
          subtitle: new Text(_projectAppID),
        ),
      ),
    );
  }
}

输出:

如果您只需要手动获取 IOS 包 ID,方法如下

  1. 在 Android Studio 中 select 根文件夹(例如 flutte_name
  2. 在任务栏中转到 Tools>>Flutter>>Open IOS Modules in Xcode
  3. 在Xcode中打开Runner,在Identity/Bundle Identifier下有你的ID

您可能想将它更新为自定义名称而不是 com.example.appName,这样您就可以在 pub.dev 上查看这个名为 change_app_name 的软件包 https://pub.dev/packages/change_app_package_name

超级简单我自己做的。将包添加到您的 pubspec 文件中,然后在根文件夹中的终端中输入“flutter pub 运行 change_app_package_name:main com.company.app”将最后一部分更改为您想要的任何内容,它将更新您的使用您选择的新名称的整个项目