了解 2023 年 Google I/O 大会上介绍的 Firebase 亮点。了解详情

App Distribution SDK を使用してアプリ内で新しいビルドアラートを設定する

Firebase App Distribution SDK(使用はオプション)を使用すると、アプリの新しいビルドがインストールに使用可能な場合に、アプリ内アラートをテスターに表示できます。このガイドでは、App Distribution SDK を使用してテスター用の新しいビルドアラートを作成し、カスタマイズする方法について説明します。

始める前に

まだ Firebase を iOS プロジェクトに追加していない場合は追加します。

ステップ 1: App Distribution Tester API を有効にする

  1. Google Cloud Platform Console でプロジェクトを選択します。

  2. [Firebase App Testers API] で [有効にする] をクリックします。

ステップ 2: App Distribution をアプリに追加する

Firebase ライブラリのインストールには CocoaPods を使用することをおすすめします。ただし、CocoaPods を使用せずに、SDK フレームワークを直接統合する方法もあります。

  1. プロジェクト用に作成した podfile を開き(または pod init を実行して作成し)、ターゲット セクション内に次の行を追加します。

    pod 'Firebase/AppDistribution'
  2. podfile のディレクトリで pod install を実行し、作成した .xcworkspace ファイルを開きます。

  3. 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
  4. Firebase モジュールを UIApplicationDelegate にインポートします。

    Swift

    import Firebase
    

    Objective-C

    @import Firebase;
    
  5. FirebaseApp 共有インスタンスを構成します。通常はアプリの application:didFinishLaunchingWithOptions: メソッドで行います。

    Swift

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

    Objective-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
    
  6. 最後に、アプリを再コンパイルします。

ステップ 3: アプリ内アラートを構成する

App Distribution SDK には、テスター用のアプリ内ビルドアラートを設定する方法が 2 つあります。基本アラート構成では、テスターに表示する事前構築済みのログイン ダイアログを使用します。詳細アラート構成では、独自のユーザー インターフェース(UI)をカスタマイズできます。SDK を初めて使用する場合は、まず基本アラート構成を試すことをおすすめします。

基本的な構成

checkForUpdate を使用して、アラートをまだ有効にしていないテスターに、事前に構築された有効化アラート ダイアログを表示し、新しいビルドが使用可能かどうかを確認します。このメソッドを呼び出すと、このメソッドにより次のシーケンスが実行されます。

  1. テスターに、Google アカウントで App Distribution にログインするように求めて、テスターがアラートを有効にしたかを確認します。

  2. テスターがまだアラートを有効にしていない場合は、事前に構築されたダイアログが表示されます。アラートの有効化はテストデバイスで 1 回限り実行するプロセスであり、アプリのアップデート以降も保持されます。有効にすると、アプリがアンインストールされるか、signOutTester メソッドが呼び出されるまで、デバイスではアラートが有効のままです(この詳細構成メソッドについては、SwiftObjective-C に関する App Distribution リファレンス ドキュメントをご覧ください)。

  3. テスターが新しくインストールできるビルドを確認します。

たとえば、次のコードでは、テスターがアラートを有効にしたか、また新しいビルドにアクセスできるかを確認します。

Swift

AppDistribution.appDistribution().checkForUpdate(completion: { release, error in
// Includes a pre-built enable alerts dialogue that lets your tester enable alerts.
// You can also customize your own user interface that prompts the tester to
// install a newly available distribution (refer to sample code below).
})

Objective-C

[[FIRAppDistribution appDistribution]
    checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
                                   NSError *_Nullable error) {
// Includes a pre-built enable alerts dialogue that lets your tester enable alerts.
// You can also customize your own user interface that prompts the tester to
// install a newly available distribution (refer to sample code below).
}];

このメソッドは、アプリに任意の時点で追加できます。たとえば、UIViewControllerviewDidAppearcheckForUpdate を含めることで、起動時に新たに使用可能なビルドをインストールするようテスターに促すことができます。

次の例では、新しいビルドがインストール可能になると、テスターにダイアログが表示されます。

Swift

AppDistribution.appDistribution().checkForUpdate(completion: { release, error in
  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) {
  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Check For Update"
message:[NSString stringWithFormat:@"Error during tester sign in! %@", error.localizedDescription]
preferredStyle:UIAlertControllerStyleAlert];

  UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {}];

  [alert addAction:okAction];
  [self presentViewController:alert animated:YES completion:nil];

  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];
  }
}];

その他のメソッドの詳細については、SwiftObjective-C の App Distribution リファレンス ドキュメントをご覧ください。

詳細構成

signInTester メソッドと isTesterSignedIn メソッドを使用すると、テスターのログイン エクスペリエンスをさらに柔軟にカスタマイズできるため、アプリのデザインに合わせることができます。

次の例では、テスターが Firebase App Distribution テスター アカウントにすでにログインしているかどうかを確認し、まだログインしていないテスターにのみログイン UI を表示するように選択できます。テスターがログインしたら、checkForUpdate を呼び出して、テスターが新しいビルドにアクセスできるかどうかを確認します。

Swift

// 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 など、その他のメソッドの詳細については、SwiftObjective-C の App Distribution リファレンス ドキュメントをご覧ください。

ステップ 4: 実装をビルドしてテストする

最後に、アプリをビルドし、Firebase コンソールを使用してテスターにビルドを配布して実装をテストします。

次のような一般的な問題については、アプリの配布に関するトラブルシューティング ガイドをご覧ください。

  • テスターがアプリ内アラートを受信できない
  • テスターが Google へのログインを複数回求められる