如要讓使用者選擇是否使用 Firebase Performance Monitoring,您可能需要設定應用程式,以便啟用及停用 Performance Monitoring。您也可能會在應用程式開發和測試期間發現這項功能很有用。
以下提供幾個建議選項:
- 您可以在建構應用程式時停用 Performance Monitoring SDK,並選擇在執行階段重新啟用。 
- 您可以建構已啟用 Performance Monitoring SDK 的應用程式,但可選擇在執行階段使用 Firebase Remote Config 停用該 SDK。 
- 您可以完全停用 Performance Monitoring SDK,且無法在執行階段啟用。 
在應用程式建構過程中停用 Performance Monitoring
在應用程式建構過程中停用 Performance Monitoring 的其中一個好處,就是避免在應用程式開發和測試期間,回報應用程式預先發布版本的效能資料。
如要停用或取消啟用 Performance Monitoring,可以將下列任一鍵新增至 Apple 應用程式的屬性清單檔案 (Info.plist):
- 如要停用 Performance Monitoring,但允許應用程式在執行階段啟用這項功能,請在應用程式的 - Info.plist檔案中將- firebase_performance_collection_enabled設為- false。
- 如要完全停用 Performance Monitoring,且無法在執行階段啟用,請在應用程式的 - Info.plist檔案中將- firebase_performance_collection_deactivated設為- true。
使用 Remote Config 在執行階段停用應用程式
Firebase Remote Config 可讓您變更應用程式的行為和外觀,因此是停用已部署應用程式執行個體中 Performance Monitoring 的理想方式。
如要在下次啟動 Apple 應用程式時停用 Performance Monitoring 資料收集功能,請使用下方的範例程式碼。如要進一步瞭解如何在 Apple 應用程式中使用 Remote Config,請參閱「在 Apple 平台上使用 Firebase Remote Config」。
- 確認 Remote Config 用於 - Podfile:- pod 'Firebase/RemoteConfig'
- 在應用程式的 - AppDelegate檔案頂端新增下列程式碼:- Swift注意:macOS、Mac Catalyst 和 watchOS 目標不支援這項 Firebase 產品。- import FirebaseRemoteConfig- Objective-C注意:macOS、Mac Catalyst 和 watchOS 目標不支援這項 Firebase 產品。- @import FirebaseRemoteConfig;
- 在 - AppDelegate檔案中,將下列程式碼新增至- application:didFinishLaunchingWithOptions:執行個體方法中的- launchOptions陳述式:- Swift注意:macOS、Mac Catalyst 和 watchOS 目標不支援這項產品。- remoteConfig = RemoteConfig.remoteConfig() // You can change the "false" below to "true" to permit more fetches when validating // your app, but you should change it back to "false" or remove this statement before // distributing your app in production. let remoteConfigSettings = RemoteConfigSettings(developerModeEnabled: false) remoteConfig.configSettings = remoteConfigSettings! // Load in-app defaults from a plist file that sets perf_disable to false until // you update values in the Firebase console. remoteConfig.setDefaultsFromPlistFileName("RemoteConfigDefaults") // Important! This needs to be applied before FirebaseApp.configure() if !remoteConfig["perf_disable"].boolValue { // The following line disables all automatic (out-of-the-box) monitoring Performance.sharedInstance().isInstrumentationEnabled = false // The following line disables all custom monitoring Performance.sharedInstance().isDataCollectionEnabled = false } else { Performance.sharedInstance().isInstrumentationEnabled = true Performance.sharedInstance().isDataCollectionEnabled = true } // Use Firebase library to configure APIs FirebaseApp.configure()- Objective-C注意:macOS、Mac Catalyst 和 watchOS 目標不支援這項 Firebase 產品。- self.remoteConfig = [FIRRemoteConfig remoteConfig]; // You can change the NO below to YES to permit more fetches when validating // your app, but you should change it back to NO or remove this statement before // distributing your app in production. FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:NO]; self.remoteConfig.configSettings = remoteConfigSettings; // Load in-app defaults from a plist file that sets perf_disable to false until // you update values in the Firebase console. [self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"]; // Important! This needs to be applied before [FIRApp configure] if (!self.remoteConfig[@"perf_disable"].numberValue.boolValue) { // The following line disables all automatic (out-of-the-box) monitoring [FIRPerformance sharedInstance].instrumentationEnabled = NO; // The following line disables all custom monitoring [FIRPerformance sharedInstance].dataCollectionEnabled = NO; } else { [FIRPerformance sharedInstance].instrumentationEnabled = YES; [FIRPerformance sharedInstance].dataCollectionEnabled = YES; } // Use Firebase library to configure APIs [FIRApp configure];
- 在 - ViewController.m或應用程式使用的其他實作檔案中,新增下列程式碼來擷取及啟用 Remote Config 值:- Swift注意:macOS、Mac Catalyst 和 watchOS 目標不支援這項 Firebase 產品。- //RemoteConfig fetch and activation in your app, shortly after startup remoteConfig.fetch(withExpirationDuration: TimeInterval(30.0)) { (status, error) -> Void in if status == .success { print("Config fetched!") self.remoteConfig.activateFetched() } else { print("Config not fetched") print("Error \(error!.localizedDescription)") } }- Objective-C注意:macOS、Mac Catalyst 和 watchOS 目標不支援這項 Firebase 產品。- //RemoteConfig fetch and activation in your app, shortly after startup [self.remoteConfig fetchWithExpirationDuration:30.0 completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) { if (status == FIRRemoteConfigFetchStatusSuccess) { NSLog(@"Config fetched!"); [self.remoteConfig activateFetched]; } else { NSLog(@"Config not fetched"); NSLog(@"Error %@", error.localizedDescription); } }];
- 如要在 Firebase 控制台中停用 Performance Monitoring,請在應用程式專案中建立 perf_disable 參數,然後將值設為 - true。- 如果將 perf_disable 的值設為 - false,Performance Monitoring 仍會保持啟用狀態。
分別停用自動或自訂資料收集功能
您可以對上述程式碼和 Firebase 控制台中的程式碼進行部分變更,以便分別停用所有自動 (隨附) 監控功能和自訂監控功能。
- 在 - application:didFinishLaunchingWithOptions:執行個體方法中,將下列程式碼新增至- launchOptions陳述式 (而非上述相同執行個體方法中顯示的內容):- Swift注意:macOS、Mac Catalyst 和 watchOS 目標不支援這項 Firebase 產品。- remoteConfig = FIRRemoteConfig.remoteConfig() let remoteConfigSettings = FIRRemoteConfigSettings(developerModeEnabled: true) remoteConfig.configSettings = remoteConfigSettings! // Important! This needs to be applied before FirebaseApp.configure() if remoteConfig["perf_disable_auto"].boolValue { // The following line disables all automatic (out-of-the-box) monitoring Performance.sharedInstance().isInstrumentationEnabled = false } else { Performance.sharedInstance().isInstrumentationEnabled = true } if remoteConfig["perf_disable_manual"].boolValue { // The following line disables all custom monitoring Performance.sharedInstance().isDataCollectionEnabled = false } else { Performance.sharedInstance().isDataCollectionEnabled = true } // Use Firebase library to configure APIs FirebaseApp.configure()- Objective-C注意:macOS、Mac Catalyst 和 watchOS 目標不支援這項 Firebase 產品。- self.remoteConfig = [FIRRemoteConfig remoteConfig]; FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES]; self.remoteConfig.configSettings = remoteConfigSettings; // Important! This needs to be applied before [FirebaseApp configure] if (self.remoteConfig[@"perf_disable_auto"].numberValue.boolValue) { // The following line disables all automatic (out-of-the-box) monitoring [FIRPerformance sharedInstance].instrumentationEnabled = NO; } else { [FIRPerformance sharedInstance].instrumentationEnabled = YES; } if (self.remoteConfig[@"perf_disable_manual"].numberValue.boolValue) { // The following line disables all custom monitoring [FIRPerformance sharedInstance].dataCollectionEnabled = NO; } else { [FIRPerformance sharedInstance].dataCollectionEnabled = YES; } // Use Firebase library to configure APIs [FirebaseApp configure];
- 在 Firebase 控制台中完成下列步驟: - 如要停用所有自動 (隨附) 監控功能,請在應用程式專案中建立 perf_disable_auto 參數,然後將值設為 true。
- 如要停用所有自訂監控功能,請在應用程式專案中建立 perf_disable_manual 參數,然後將值設為 true。
 
- 如要停用所有自動 (隨附) 監控功能,請在應用程式專案中建立 perf_disable_auto 參數,然後將值設為