測試 Crashlytics 實作

強制執行當機以測試實作

  1. 在應用程式中加入程式碼,以便強制測試當機。

    您可以使用以下程式碼,在應用程式中新增按鈕,以便在按下時造成當機。這個按鈕標示為「Test Crash」

    SwiftUI

    Button("Crash") {
      fatalError("Crash was triggered")
    }
    

    UIKit

    Swift

    import UIKit
    
    class ViewController: UIViewController {
      override func viewDidLoad() {
          super.viewDidLoad()
    
          // Do any additional setup after loading the view, typically from a nib.
    
          let button = UIButton(type: .roundedRect)
          button.frame = CGRect(x: 20, y: 50, width: 100, height: 30)
          button.setTitle("Test Crash", for: [])
          button.addTarget(self, action: #selector(self.crashButtonTapped(_:)), for: .touchUpInside)
          view.addSubview(button)
      }
    
      @IBAction func crashButtonTapped(_ sender: AnyObject) {
          let numbers = [0]
          let _ = numbers[1]
      }
    }
    

    Objective-C

    #import "ViewController.h"
    
    @implementation ViewController
    ‐ (void)viewDidLoad {
        [super viewDidLoad];
    
        // Do any additional setup after loading the view, typically from a nib.
    
        UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(20, 50, 100, 30);
        [button setTitle:@"Test Crash" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(crashButtonTapped:)
            forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
    ‐ (IBAction)crashButtonTapped:(id)sender {
        @[][1];
    }
    
    @end
    
  2. 中斷 Xcode 偵錯工具,在 Xcode 中建構及執行應用程式。

    1. 按一下 「Build and run the current scheme」(建構目前的配置),然後在測試裝置或模擬器上建構應用程式。

    2. 等待應用程式執行完畢,然後按一下 「Stop running the scheme or action」(停止執行配置或動作),關閉應用程式的初始執行個體。這個初始執行個體中包含會幹擾 Crashlytics 的偵錯工具。

  3. 強制測試當機,以便傳送應用程式的第一份當機報告:

    1. 從測試裝置或模擬器的主畫面開啟應用程式。

    2. 在應用程式中,按下使用上述程式碼新增的「Test Crash」按鈕。

    3. 應用程式當機後,請透過 Xcode 再次執行,讓應用程式能夠將當機報告傳送至 Firebase。

  4. 前往 Firebase 控制台的 Crashlytics 資訊主頁,查看測試當機情形。

如果您已重新整理主控台,但五分鐘後仍未看到測試當機情形,請嘗試啟用偵錯記錄 (下一節)。

啟用 Crashlytics 的偵錯記錄功能

如果 Crashlytics 資訊主頁未顯示測試當機事件,您可以使用 Crashlytics 的偵錯記錄功能來協助追蹤問題。

  1. 啟用偵錯記錄功能:

    1. 在 Xcode 中,依序選取「Product」>「 Scheme」>「Edit scheme」

    2. 選取左選單中的「Run」,然後選取「Arguments」分頁標籤。

    3. 在「Arguments Passed on Launch」區段中,新增 -FIRDebugEnabled

  2. 強制執行測試當機。本頁第一部分將說明如何進行這項操作。

  3. 在記錄檔中,搜尋包含下列字串的 Crashlytics 記錄訊息,確認應用程式正在傳送當機事件至 Firebase。

    Completed report submission

如果經過五分鐘後,如果 Firebase 控制台的 Crashlytics 資訊主頁未顯示此記錄或測試停止運作,請與 Firebase 支援團隊聯絡,並提供記錄檔輸出內容的副本,以便我們協助您進一步排解問題。

後續步驟