美文网首页
package_info_plus-iOS获取原生中定义的Ver

package_info_plus-iOS获取原生中定义的Ver

作者: QYCD | 来源:发表于2023-05-29 15:37 被阅读0次

    package_info_plus

    flutter环境:

    cc@CCdeMacBook-Pro-2 ~ % flutter doctor
    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel stable, 3.7.6, on macOS 13.3.1 22E772610a darwin-arm64,
        locale zh-Hans-CN)
    [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    [✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    [✓] Chrome - develop for the web
    [✓] Android Studio (version 2022.1)
    [✓] VS Code (version 1.76.0)
    [✓] Connected device (6 available)
    [✓] HTTP Host Availability
    
    • No issues found!
    
    最新版本.png
    1. pubspec.yaml中依赖package_info_plus
    package_info_plus: 4.0.2
    
    1. 执行下flutter pub get

    2. 获取所需应用信息

    import 'package:package_info_plus/package_info_plus.dart';
    
    ...
    
    // Be sure to add this line if `PackageInfo.fromPlatform()` is called before runApp()
    WidgetsFlutterBinding.ensureInitialized();
    
    ...
    
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    
    String appName = packageInfo.appName;
    String packageName = packageInfo.packageName;
    String version = packageInfo.version;
    String buildNumber = packageInfo.buildNumber;
    

    以上按照插件文档所讲即可。

    pubspec.yaml中:

    version: 1.0.0+1
    

    安卓获取的version对应的为pubspec.yaml中version中"+"号前面,buildNumber为version中"+"后面的。

    这里主要说下iOS端获取原生端定义的Version和Build值:
    新建flutter项目后,查看iOS原生端info.plist文件的定义,其中Bundle version string (short)的值为: (FLUTTER_BUILD_NAME)、Bundle version的值为(FLUTTER_BUILD_NUMBER)

    info.plist.png

    而这时我们使用package_info_plus获取的version和buildNumber得到的值是flutter端pubspec.yaml中version的值,与安卓表现一致。

    但是鉴于一个flutter项目,iOS与安卓版本号和build的值并不一定一致,对于iOS希望获取原生端定义的Version和Build,我们将info.plist中Bundle version string (short)和Bundle version的值修改为获取原生端的值即可。

    Bundle version string (short): $(MARKETING_VERSION)
    Bundle version: $(CURRENT_PROJECT_VERSION)
    
    image.png

    如此,iOS使用package_info_plus获取的version和buildNumber将是原生端定义的Version和Build

    相关文章

      网友评论

          本文标题:package_info_plus-iOS获取原生中定义的Ver

          本文链接:https://www.haomeiwen.com/subject/viijedtx.html