可选的 Firebase App Distribution iOS 和 Android SDK 可让您在可以安装应用的新版本时向测试人员显示应用内警报。本指南介绍了如何使用 App Distribution iOS 和 Android SDK 为您的测试人员创建和自定义新的构建警报。
在你开始之前
如果您还没有,请将 Firebase 添加到您的 iOS 项目中。
第 1 步:启用 App Distribution Tester API
在Google Cloud Console中选择您的项目。
在Firebase App Testers API下,点击Enable 。
第 2 步:将应用分发添加到您的应用
打开您为项目创建的 Podfile(或运行
pod init
创建一个),然后在目标部分添加以下行:pod 'FirebaseAppDistribution'
在您的 podfile 目录中,运行
pod install
,然后打开创建的.xcworkspace
文件。对您的 Google 应用 ID 进行编码(仅 iOS 版本 9 和 10 需要):
对您的 Google 应用 ID 进行编码
通过在
Info.plist file
中包含代码段来添加appdistribution-<encoded-google-app-id>
URL 方案(有关如何在 Xcode 中添加 URL 方案的说明,请参阅Apple 的文档):<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>appdistribution-<encoded-google-app-id></string> </array> </dict> </array>
然后,通过将冒号 (:) 替换为破折号 (-) 来对您的 Google 应用 ID 进行编码。请注意,您的 Google 应用 ID 位于您的
GoogleService-Info.plist
文件中。例如,如果您的 Google 应用 ID 是:7:77777777777:ios:123456789
您的编码 Google 应用 ID 为:
7-77777777777-ios-123456789
在您的
App
结构或UIApplicationDelegate
中导入 Firebase 模块:迅速
import FirebaseCore
Objective-C
@import FirebaseCore;
配置
FirebaseApp
共享实例,通常在App
的初始化程序或应用委托的application(_:didFinishLaunchingWithOptions:)
方法中:迅速
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
最后,重新编译您的应用程序。
第 3 步:配置应用内警报
App Distribution SDK 提供了两种为测试人员设置应用内构建警报的方法:基本警报配置,带有预构建的应用更新和登录对话框以显示给测试人员,以及高级警报配置,允许您可以自定义自己的用户界面。如果您是 App Distribution SDK 的新手,我们建议您首先使用基本警报配置。
基本配置
使用checkForUpdate
向尚未启用警报的测试人员显示预构建的启用警报对话框,然后检查是否有新的构建可用。调用时,该方法执行以下序列:
通过提示测试人员使用其 Google 帐户登录 App Distribution 来检查测试人员是否启用了警报。
如果测试人员尚未启用警报,则会显示一个预先构建的对话框。
启用警报是测试设备上的一次性过程,并且在您的应用程序更新期间持续存在。在卸载应用程序或调用
signOutTester
方法之前,测试设备上的警报将保持启用状态。有关更多信息,请参阅该方法的参考文档( Swift或Objective-C )。检查新可用的构建以供测试人员安装。
您可以在应用程序中的任何位置调用checkForUpdate()
。例如,您可以通过在应用程序根视图的onAppear(perform:)
中包含checkForUpdate()
来提示测试人员在启动时安装新的可用构建。
以下示例检查测试人员是否已启用警报并有权访问新构建,如果是,则在构建可安装时显示对话框:
迅速
AppDistribution.appDistribution().checkForUpdate(completion: { release, error in
if error != nil {
// Handle error
return
}
guard let release = release else {
return
}
// Customize your alerts here.
let title = "New Version Available"
let message = "Version \(release.displayVersion)(\(release.buildVersion)) is available."
let uialert = UIAlertController(title: title,message: message, preferredStyle: .alert)
uialert.addAction(UIAlertAction(title: "Update", style: UIAlertAction.Style.default) {
_ in
UIApplication.shared.open(release.downloadURL)
})
uialert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) {
_ in
})
// self should be a UIViewController.
self.present(uialert, animated: true, completion: nil)
})
Objective-C
[[FIRAppDistribution appDistribution]
checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
NSError *_Nullable error) {
if (error) {
// Handle error
return;
}
if (release) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"New Version Available"
message:[NSString stringWithFormat:@"Version %@ (%@) is available.", release.displayVersion,
release.buildVersion] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *updateAction = [UIAlertAction actionWithTitle:@"Update"
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:release.downloadURL options:@{}
completionHandler:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
[alert addAction:updateAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}
}];
高级配置
方法signInTester()
和isTesterSignedIn
为您提供了更大的灵活性来自定义测试人员的登录体验,因此它可以更好地匹配您的应用程序的外观和感觉。
以下示例检查测试人员是否已登录其 Firebase App Distribution 测试人员帐户,因此您可以选择仅为尚未登录的测试人员显示您的登录 UI。测试人员登录后,您可以然后调用checkForUpdate()
来检查测试人员是否可以访问新版本。
迅速
// Sign in a tester without automatically checking for update
if (!AppDistribution.appDistribution().isTesterSignedIn) {
AppDistribution.appDistribution().signInTester (completion: { error in
// completion block for signInTester
if (error != nil) {
// handle failed sign in
return
}
// handle successful sign in
})
}
// Only check for update if tester is already signed in - do not prompt
if (AppDistribution.appDistribution().isTesterSignedIn) {
AppDistribution.appDistribution().checkForUpdate(completion: { release, error in
// completion block for check for update
})
}
Objective-C
// Sign in a tester without automatically checking for update
if(![[FIRAppDistribution appDistribution] isTesterSignedIn]) {
[[FIRAppDistribution appDistribution]
signInTesterWithCompletion:^(NSError *_Nullable error) {
// completion block for signInTester
if (error) {
// handle failed sign in
return;
}
// handle successful sign in
}];
}
// only check for update if tester is already signed in - do not prompt
if([[FIRAppDistribution appDistribution] isTesterSignedIn]) {
[[FIRAppDistribution appDistribution]
checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
NSError *_Nullable error) {
// completion block for check for update
}];
}
有关其他方法的信息,包括signOutTester()
,请参阅Swift和Objective-C的 App Distribution 参考文档。
第 4 步:构建和测试您的实现
最后,通过使用 Firebase 控制台将构建分发给测试人员来构建您的应用并测试您的实施。
访问应用程序分发故障排除指南以获取有关常见问题的帮助,例如:
- 测试人员未收到应用内警报
- 多次提示测试人员登录 Google