可選的 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