Firebase Crashlytics 是一款精簡的即時當機回報工具,可讓您輕鬆管理應用程式的穩定性問題。Crashlytics 可以巧妙地將當機事件分組,並醒目顯示引發這些當機情況的情況,藉此節省疑難排解時間。
本指南說明如何將 Crashlytics 整合至 Xcode 專案,以便記錄廣告回應 ID。之後當您排解應用程式中的當機問題時,可以查詢廣告回應 ID,並使用 AdMob 找出並封鎖廣告。
步驟 1:將 Firebase 新增至 iOS 應用程式
如要從乾淨的應用程式中使用 Firebase 記錄,您可以前往 GitHub 下載或複製 iOS 存放區適用的 Google Mobile Ads SDK 範例。本指南專門說明 橫幅廣告範例。
如果您已有應用程式,則應使用應用程式的軟體包 ID 繼續完成其他步驟。相同的步驟也適用於存放區中的其他範例,但只需稍微調整。
如要使用 Firebase Crashlytics,您必須建立 Firebase 專案並加入應用程式。如果您尚未建立 Firebase 專案,請先建立。請務必註冊應用程式。
在 Firebase 控制台的「Crashlytics」頁面中,按一下「Set up Crashlytics」。
在隨即顯示的畫面中,依序按一下「No」>「Set a new Firebase app」。
在 Podfile 中,新增用於 Google Analytics (分析) 和 Firebase Crashlytics 的 Pod。
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' target 'BannerExample' do use_frameworks! pod 'Google-Mobile-Ads-SDK' pod 'Firebase/Crashlytics' pod 'Firebase/Analytics' end
在終端機或命令提示字元中,安裝及更新 Pod:
pod install --repo-update
請開啟 Xcode 的
BannerExample.xcworkspace
檔案來載入專案。
步驟 2:為應用程式設定 Firebase
Swift
在 AppDelegate.swift
中加入下列程式碼:
import UIKit // Import the Firebase library import FirebaseCore @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Configure an instance of Firebase FirebaseApp.configure() return true } }
目標 C
在 AppDelegate.m
中加入下列程式碼:
@import AppDelegate.h; // Import the Firebase library @import FirebaseCore; @interface AppDelegate () @end @implementation AppDelegate ‐ (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Initialize Firebase [FIRApp configure]; return YES; }
在 Xcode 中開啟「Build Settings」,然後按一下「Build Phases」分頁標籤。新增 Fabric 執行指令碼:
清理建構資料夾,然後建構並執行應用程式。現在您可以登入 Firebase 網路控制台,並存取 Crashlytics 資訊主頁。
(選用):測試設定
新增當機按鈕,即可在每次按下按鈕時強製造成應用程式當機。這項測試設定會觸發步驟 3 中的程式碼,將自訂記錄傳送至 Firebase Crashlytic 資訊主頁。
Swift
在 ViewController.swift
中,將下列程式碼新增到 viewDidLoad()
函式中:
override func viewDidLoad() { super.viewDidLoad() print("Google Mobile Ads SDK version: \(GADRequest.sdkVersion())") bannerView.delegate = self bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716" bannerView.rootViewController = self bannerView.load(GADRequest()) let button = UIButton(type: .roundedRect) button.frame = CGRect(x: 20, y: 50, width: 100, height: 30) button.setTitle("Crash", for: []) button.addTarget(self, action: #selector(self.crashButtonTapped(_:)), for: .touchUpInside) view.addSubview(button) }
然後在類別宣告底部加入 @IBAction
:
@IBAction func crashButtonTapped(_ sender: AnyObject) { fatalError("Test Crash Happened") }
目標 C
在 ViewController.m
的 viewDidLoad
方法中新增以下幾行內容:
‐ (void)viewDidLoad { [super viewDidLoad]; /// ... UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(20, 50, 100, 30); [button setTitle:@"Crash" forState:UIControlStateNormal]; [button addTarget:self action:@selector(crashButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; }
然後在類別宣告底部加入 IBAction
:
‐ (IBAction)crashButtonTapped:(id)sender { assert(NO); }
在 Xcode 工具列中,按下「Stop」按鈕,然後透過模擬器重新啟動應用程式。應用程式載入後,您可以按一下「Crash」按鈕。返回 Xcode 並點選「Play」按鈕,即可將當機記錄上傳至 Crashlytics。
步驟 3:記錄廣告回應 ID
如要載入多則廣告並在不同時間顯示,建議您使用個別金鑰記錄每個廣告回應 ID。例如,本指南使用只包含一則橫幅廣告的範例。因此,我們會在以下程式碼片段中,將廣告回應 ID 記錄為 banner_ad_response_id
鍵。
您也可以在 Firebase Crashlytics 中,為不同的廣告類型和廣告事件建立多個自訂鍵 / 值組合。要查看廣告請求生命週期通知
如要進一步瞭解自訂記錄,請造訪「自訂 Firebase Crashlytics 當機報告」。
Swift
將下列程式碼新增到 ViewController.swift
。基本上,這個呼叫會使用 adViewDidReceiveAd
回呼函式中的 Crashlytics.setCustomValue()
函式。
import GoogleMobileAds import UIKit class ViewController: UIViewController, GADBannerViewDelegate { /// The banner view. @IBOutlet weak var bannerView: GADBannerView! override func viewDidLoad() { super.viewDidLoad() ... bannerView.delegate = self ... } /// Tells the delegate an ad request loaded an ad. func adViewDidReceiveAd(_ bannerView: GADBannerView) { if let responseInfo = bannerView.responseInfo, responseId = responseInfo.responseId { print("adViewDidReceiveAd from network: \(responseInfo.adNetworkClassName), response Id='\(responseId)'") Crashlytics.sharedInstance().setCustomValue(responseId, forKey: "banner_ad_response_id") } } }
目標 C
將下列程式碼新增到 ViewController.m
。基本上,這種函式會使用 adViewDidReceiveAd
函式中的 [FIRCrashlytics crashlytics]
setCustomValue
函式。
@import GoogleMobileAds; @interface ViewController () @property(nonatomic, strong) GADBannerView *bannerView; @end @implementation ViewController ‐ (void)viewDidLoad { [super viewDidLoad]; // In this case, we instantiate the banner with desired ad size. self.bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeBanner]; [self addBannerViewToView:self.bannerView]; } ‐ (void)addBannerViewToView:(UIView *)bannerView { bannerView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:bannerView]; [self.view addConstraints:@[ [NSLayoutConstraint constraintWithItem:bannerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1 constant:0], [NSLayoutConstraint constraintWithItem:bannerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view kattribute:NSLayoutAttributeCenterX multiplier:1 constant:0] ]]; } - (void)adViewDidReceiveAd:(GADBannerView *)bannerView { NSString *adResponseId = bannerView.responseInfo.responseId; if (adResponseId) { NSLog(@"adViewDidReceiveAd from network: %@ with response Id: %@", bannerView.responseInfo.adNetworkClassName, adResponseId); [[FIRCrashlytics crashlytics] setCustomValue:adResponseId forKey:@"banner_ad_response_id"]; } } @end
恭喜!您現在可以在 Crashlytics 資訊主頁的主要當機工作階段主要部分,查看最新的 adResponseId
。請注意,部分金鑰最多可能要過一小時才會顯示在資訊主頁中。