Flutter应用更新插件pgyer_updater的使用

Flutter应用更新插件pgyer_updater的使用

pgyer_updater 是一个用于 Flutter 应用的蒲公英应用更新插件。它通过 API 实现,理论上不受平台限制。该插件的主要功能包括弹窗提醒用户有新版本可用,并提供下载链接,开发者可以自行实现下载逻辑。

示例代码

以下是一个完整的示例代码,展示如何使用 pgyer_updater 插件来检查更新并提示用户。

import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart'; // 用于获取当前应用版本信息
import 'package:pgyer_updater/pgyer_updater.dart'; // 引入pgyer_updater插件

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: UpdateCheckPage(),
    );
  }
}

class UpdateCheckPage extends StatefulWidget {
  @override
  _UpdateCheckPageState createState() => _UpdateCheckPageState();
}

class _UpdateCheckPageState extends State<UpdateCheckPage> {
  Future<void> checkForUpdates() async {
    // 获取当前应用的版本信息
    final packageInfo = await PackageInfo.fromPlatform();
    final currentVersion = packageInfo.version;

    // 调用 pgyer_updater 的 check 方法检查更新
    final updateResult = await PgyerUpdater.check(
      apiKey: "your_api_key_here", // 替换为您的蒲公英 API Key
      appKey: "your_app_key_here", // 替换为您的蒲公英应用 Key
      versionName: currentVersion, // 当前版本号
    );

    // 如果有更新,弹出更新对话框
    if (updateResult != null && mounted) {
      PgyerUpdater.showUpdateDialog(
        context,
        versionResult: updateResult,
        onConfirm: (appUrl) {
          // 用户点击确认按钮时触发
          // 可以在这里实现下载逻辑
          print("下载地址: $appUrl");
          // launchUrlString(appUrl); // 打开下载链接
          // 或者手动下载文件
          // final downloadUrl = PgyerUpdater.getDownloadUrl(updateResult);
          // download(downloadUrl);
        },
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('应用更新示例'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: checkForUpdates,
          child: Text('检查更新'),
        ),
      ),
    );
  }
}

代码说明

  1. 引入依赖

    • 使用 package_info_plus 插件获取当前应用的版本信息。
    • 使用 pgyer_updater 插件进行版本检查和更新提示。
  2. 检查更新逻辑

    • checkForUpdates 方法中,首先通过 PackageInfo.fromPlatform() 获取当前应用的版本号。
    • 然后调用 PgyerUpdater.check() 方法,传入 API Key、应用 Key 和当前版本号,检查是否有新版本可用。
  3. 弹出更新对话框

    • 如果检测到有新版本,调用 PgyerUpdater.showUpdateDialog() 显示更新提示对话框。
    • 对话框包含更新日志、下载链接等信息,用户可以选择立即更新或稍后更新。
  4. 下载逻辑

    • onConfirm 回调函数中,可以处理下载逻辑。例如,使用 launchUrlString() 打开下载链接,或者通过 PgyerUpdater.getDownloadUrl() 获取下载 URL 并手动下载。

运行效果

运行上述代码后,点击屏幕上的“检查更新”按钮,程序会自动检查是否有新版本可用。如果有新版本,会弹出一个对话框,提示用户下载并安装最新版本的应用。

注意事项

  • 替换代码中的 your_api_key_hereyour_app_key_here 为实际的蒲公英 API Key 和应用 Key。
  • 确保在 pubspec.yaml 文件中添加了必要的依赖项:
    dependencies:
      package_info_plus: ^0.4.0
      pgyer_updater: ^1.0.0
    

更多关于Flutter应用更新插件pgyer_updater的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用更新插件pgyer_updater的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


pgyer_updater 是一个用于 Flutter 应用的插件,可以帮助你轻松集成蒲公英(Pgyer)的应用更新功能。蒲公英是一个移动应用内测分发平台,通过 pgyer_updater 插件,你可以让你的应用在启动时自动检查更新,并提示用户下载新版本。

以下是如何在 Flutter 应用中使用 pgyer_updater 插件的详细步骤:

1. 添加依赖

首先,在你的 pubspec.yaml 文件中添加 pgyer_updater 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  pgyer_updater: ^0.0.1 # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 初始化插件

在你的 main.dart 文件中初始化 pgyer_updater 插件。通常,你会在 main 函数中进行初始化:

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 PgyerUpdater
  await PgyerUpdater.init(
    apiKey: 'your_api_key', // 替换为你的蒲公英 API Key
    appKey: 'your_app_key',  // 替换为你的蒲公英 App Key
  );

  runApp(MyApp());
}

3. 检查更新

你可以在应用启动时或其他合适的地方调用 checkUpdate 方法来检查更新。以下是一个简单的示例:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    _checkForUpdate();
  }

  Future<void> _checkForUpdate() async {
    try {
      final updateInfo = await PgyerUpdater.checkUpdate();
      if (updateInfo != null && updateInfo.hasUpdate) {
        // 提示用户有新版本
        _showUpdateDialog(updateInfo);
      }
    } catch (e) {
      print('检查更新失败: $e');
    }
  }

  void _showUpdateDialog(UpdateInfo updateInfo) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('发现新版本'),
          content: Text('当前版本: ${updateInfo.currentVersion}\n最新版本: ${updateInfo.latestVersion}'),
          actions: <Widget>[
            TextButton(
              child: Text('稍后再说'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text('立即更新'),
              onPressed: () {
                PgyerUpdater.downloadAndInstall();
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Pgyer Updater Demo'),
      ),
      body: Center(
        child: Text('检查更新中...'),
      ),
    );
  }
}

4. 处理更新

_showUpdateDialog 方法中,我们展示了一个对话框,提示用户有新版本可用。如果用户选择“立即更新”,则调用 PgyerUpdater.downloadAndInstall() 方法下载并安装新版本。

5. 配置 Android 和 iOS

为了确保更新功能在 Android 和 iOS 上正常工作,你可能需要做一些额外的配置:

Android

AndroidManifest.xml 中添加以下权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

iOS

Info.plist 中添加以下权限:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>
回到顶部