【问题标题】:How to get bundle id in flutter如何在颤动中获取捆绑包ID
【发布时间】:2018-12-08 10:20:22
【问题描述】:

我使用下面的方法来获取应用程序名称和 packageName,但我需要 iPhone 用户的 Bundle id。 我想分享一个应用链接。 我是在 android 上完成的,但在 iPhone 上,我需要 bundle id。

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

【问题讨论】:

  • info.packageName 应该可以在 iOS 上运行。不是吗?

标签: ios flutter dart bundle-identifier package-name


【解决方案1】:

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

安卓

在 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;

另见

【讨论】:

    【解决方案2】:

    在 Flutter 项目的 iOS 部分中,Product Bundle Identifier 位于路径中的project.pbxproj 文件中:

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

    具体如下:

    PRODUCT_BUNDLE_IDENTIFIER = com.app.flutter.example;
    

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

    【讨论】:

      【解决方案3】:

      您可以在iOS和Android上使用get_version 包获取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),
                    ),
                  ],
                ),
              ),
            ),
          );
        }
      }
      

      【讨论】:

        【解决方案4】:

        如果您只需要手动获取 IOS 捆绑包 ID,请参考以下方法

        1. 在 Android Studio 中选择根文件夹(例如flutte_name
        2. 在任务栏中转到Tools&gt;&gt;Flutter&gt;&gt;Open IOS Modules in Xcode
        3. 在 Xcode open Runner 和 Identity/Bundle Identifier 下有你的 ID

        【讨论】:

          【解决方案5】:

          使用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.';
          }
          

          你可以在任何你想要的东西中使用它作为字符串,比如文本小部件等......


          另一个小应用程序中的 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),
                  ),
                ),
              );
            }
          }
          

          输出:

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-02-10
            • 1970-01-01
            • 2010-10-15
            • 2014-04-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-09-21
            相关资源
            最近更新 更多