Flutter应用分发插件pgyer_api的使用
Flutter应用分发插件pgyer_api的使用
pgyer_api
用于通过接口的方式上传应用到蒲公英。
安装
在 pubspec.yaml
文件中添加以下依赖:
dependencies:
pgyer_api: ^版本号
然后运行以下命令安装依赖:
dart pub get
使用
上传应用一共分为三个步骤:
第一步:获取上传应用的信息
调用 GetCosToken
获取上传所需的参数。
import 'package:pgyer_api/pgyer_api.dart';
void main() async {
// 替换为你的蒲公英 API 密钥
final String apiKey = 'your_api_key';
// 调用获取上传信息接口
final response = await PgyerApiV2().request(GetCosToken(
apiKey: apiKey,
buildType: 'ios', // ios 或 android
));
// 打印返回结果
print('上传所需信息: $response');
}
第二步:上传应用
使用第一步获取的参数上传应用文件。
import 'dart:io';
import 'package:pgyer_api/pgyer_api.dart';
void main() async {
// 替换为你的蒲公英 API 密钥
final String apiKey = 'your_api_key';
// 调用获取上传信息接口
final GetCosTokenResponse cosTokenResponse = await PgyerApiV2().request(GetCosToken(
apiKey: apiKey,
buildType: 'ios', // ios 或 android
));
// 应用文件路径(替换为实际路径)
final File ipa = File('/path/to/your/app.ipa');
// 上传应用文件
final uploadResponse = await PgyerApiV2().request(UploadFile(
endpoint: cosTokenResponse.endpoint,
key: cosTokenResponse.key,
signature: cosTokenResponse.params['signature'],
xCosSecurityToken: cosTokenResponse.params['x-cos-security-token'],
file: ipa, // 上传的应用文件
));
// 打印上传结果
print('上传结果: $uploadResponse');
}
第三步:检测应用是否上传成功
调用 BuildInfo
检查上传状态。
import 'package:pgyer_api/pgyer_api.dart';
void main() async {
// 替换为你的蒲公英 API 密钥
final String apiKey = 'your_api_key';
// 替换为第一步获取的 BuildKey
final String buildKey = 'your_build_key';
// 检测上传状态
final buildInfoResponse = await PgyerApiV2().request(BuildInfo(
apiKey: apiKey,
buildKey: buildKey,
));
// 打印检测结果
print('上传状态: $buildInfoResponse');
}
参数说明
具体参数信息请参考代码注释或蒲公英官方文档:蒲公英API文档。
示例代码
完整的示例代码可以在以下文件中查看:
example/pgyer_api_example.dart
以下是示例代码的完整结构:
import 'dart:io';
import 'package:pgyer_api/pgyer_api.dart';
void main() async {
// 替换为你的蒲公英 API 密钥
final String apiKey = 'your_api_key';
// 替换为你的应用文件路径
final File ipa = File('/path/to/your/app.ipa');
try {
// 第一步:获取上传信息
final GetCosTokenResponse cosTokenResponse = await PgyerApiV2().request(GetCosToken(
apiKey: apiKey,
buildType: 'ios', // ios 或 android
));
print('上传信息: $cosTokenResponse');
// 第二步:上传应用文件
final UploadFileResponse uploadResponse = await PgyerApiV2().request(UploadFile(
endpoint: cosTokenResponse.endpoint,
key: cosTokenResponse.key,
signature: cosTokenResponse.params['signature'],
xCosSecurityToken: cosTokenResponse.params['x-cos-security-token'],
file: ipa,
));
print('上传结果: $uploadResponse');
// 第三步:检测上传状态
final BuildInfoResponse buildInfoResponse = await PgyerApiV2().request(BuildInfo(
apiKey: apiKey,
buildKey: cosTokenResponse.key,
));
print('上传状态: $buildInfoResponse');
} catch (e) {
print('发生错误: $e');
}
}
更多关于Flutter应用分发插件pgyer_api的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter应用分发插件pgyer_api的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
pgyer_api
是一个用于将 Flutter 应用发布到蒲公英(Pgyer)平台的插件。蒲公英是一个提供应用内测分发的平台,开发者可以通过它快速将应用分发给测试人员。使用 pgyer_api
插件,你可以在 Flutter 项目中自动化应用的分发过程。
以下是如何使用 pgyer_api
插件的详细步骤:
1. 安装 pgyer_api
插件
首先,你需要在 pubspec.yaml
文件中添加 pgyer_api
插件的依赖:
dependencies:
flutter:
sdk: flutter
pgyer_api: ^1.0.0 # 请查看最新版本
然后运行 flutter pub get
来安装插件。
2. 获取蒲公英 API Key
在使用 pgyer_api
之前,你需要在蒲公英平台上获取 API Key。登录蒲公英后,进入API 文档页面,找到你的 API Key。
3. 使用 pgyer_api
发布应用
在你的 Flutter 项目中,你可以使用 pgyer_api
来发布应用。以下是一个简单的示例:
import 'package:pgyer_api/pgyer_api.dart';
void main() async {
// 初始化 PgyerApi,传入你的 API Key
final pgyerApi = PgyerApi(apiKey: 'your_api_key');
// 上传应用
final response = await pgyerApi.uploadApp(
filePath: '/path/to/your/app.apk', // 应用的路径
buildInstallType: 1, // 安装类型,1: 公开,2: 密码安装
buildPassword: '123456', // 安装密码(如果需要)
buildUpdateDescription: 'New release', // 更新描述
);
// 检查上传结果
if (response.isSuccess) {
print('App uploaded successfully!');
print('App URL: ${response.buildShortcutUrl}');
} else {
print('Failed to upload app: ${response.errorMessage}');
}
}