Catch up on highlights from Firebase at Google I/O 2023. Learn more

通知測試人員有關新版本的信息

可選的 Firebase App Distribution iOS 和 Android SDK 讓您可以在您的應用程序的新版本可供安裝時向您的測試人員顯示應用程序內警報。本指南介紹瞭如何使用 App Distribution iOS 和 Android SDK 為您的測試人員創建和自定義新的構建警報。

在你開始之前

如果您還沒有,請將 Firebase 添加到您的 iOS 項目中

第 1 步:啟用 App Distribution Tester API

  1. Google Cloud Console中選擇您的項目。

  2. Firebase App Testers API下,單擊啟用

第 2 步:將 App Distribution 添加到您的應用

  1. 打開您為項目創建的 Podfile(或運行pod init創建一個),然後在目標部分中添加以下行:

    pod 'FirebaseAppDistribution'
  2. 在 podfile 的目錄中,運行pod install ,然後打開創建的.xcworkspace文件。

  3. 在您的App結構或UIApplicationDelegate中導入 Firebase 模塊:

    迅速

    import FirebaseCore
    import FirebaseAppDistribution
    

    目標-C

    @import FirebaseCore;
    @import FirebaseAppDistribution;
    
  4. 在您的應用委託的application(_:didFinishLaunchingWithOptions:)方法中配置一個FirebaseApp共享實例:

    迅速

    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    

    目標-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
    
  5. 如果 swizzling 被禁用,請在您的application(_:open:options:)實現中將任何打開的 URL 傳遞給 App Distribution SDK:

    迅速

    func application(_ app: UIApplication, 
                     open url: URL,
                     options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
       if AppDistribution.appDistribution().application(application, open: url, options: options) {
          return true
       }
    
       // Handle other non-Firebase URLs here.
    
       return false
    }
    

    目標-C

    - (BOOL)application:(UIApplication *)app 
                openURL:(NSURL *)url 
                options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
       if ([[FIRAppDistribution appDistribution] application:app openURL:url options:options]) {
          return YES;
       }
    
       // Handle other non-Firebase URLs here.
    
       return NO;
    }
    
  6. 最後,重新編譯您的應用程序。

第 3 步:配置應用內提醒

App Distribution SDK 提供了兩種為測試人員設置應用程序內構建警報的方法:一種是基本警報配置,其中包含預構建的應用程序更新和登錄對話框以顯示給測試人員,另一種是高級警報配置,它允許您可以自定義您自己的用戶界面。如果您不熟悉 App Distribution SDK,我們建議您首先使用基本警報配置。

基本配置

使用checkForUpdate向尚未啟用警報的測試人員顯示預構建的啟用警報對話框,然後檢查是否有新版本可用。調用時,該方法執行以下序列:

  1. 通過提示測試人員使用其 Google 帳戶登錄 App Distribution 來檢查測試人員是否已啟用警報。

  2. 如果測試人員尚未啟用警報,則顯示預建對話。

    啟用警報是測試設備上的一次性過程,並且在您的應用程序更新期間持續存在。在卸載應用程序或調用signOutTester方法之前,警報在測試設備上保持啟用狀態。有關詳細信息,請參閱方法的參考文檔( SwiftObjective-C )。

  3. 檢查新的可用構建以供測試人員安裝。

您可以在應用程序的任何位置調用checkForUpdate() 。例如,您可以通過在應用根視圖的onAppear(perform:)中包含checkForUpdate()來提示您的測試人員在啟動時安裝新的可用構建。

以下示例檢查測試人員是否已啟用警報並有權訪問新構建,如果是,則在構建可用於安裝時顯示對話框:

迅速

注意:此產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。
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)
})

目標-C

注意:此產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。
[[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()檢查測試人員是否有權訪問新構建。

迅速

注意:此產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。
// 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
  })
}

目標-C

注意:此產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。
// 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()的信息,請參閱SwiftObjective-C的 App Distribution 參考文檔。

第 4 步:構建和測試您的實施

最後,構建您的應用程序並通過使用 Firebase 控制台將構建分發給測試人員來測試您的實施。

請訪問App Distribution Troubleshooting 指南以獲取有關常見問題的幫助,例如:

  • 測試人員未收到應用內提醒
  • 系統多次提示測試人員登錄 Google