如需允许用户选择是否使用 Firebase Performance Monitoring,您需要对应用进行配置,以便能启用和停用 Performance Monitoring。这一功能在应用开发和测试期间可能也有用。
请考虑以下可选方案:
- 您可以在构建应用时停用 Performance Monitoring SDK 并提供在运行时重新启用该 SDK 的选项。 
- 您可以在构建应用时启用 Performance Monitoring SDK 并提供在运行时停用该 SDK 的选项(使用 Firebase Remote Config)。 
- 您可以完全停用 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,利用 Firebase Remote Config 是一种理想的方式。
如需在 Apple 应用下次启动时停用 Performance Monitoring 数据收集,请使用下面的示例代码。如需详细了解如何在 Apple 应用中使用 Remote Config,请参阅在 Apple 平台上使用 Firebase Remote Config。
- 确保在 - Podfile中使用 Remote Config:- pod 'Firebase/RemoteConfig'
- 在应用的 - AppDelegate文件的顶部添加以下内容:- Swift注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标平台。- import FirebaseRemoteConfig- Objective-C注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标平台。- @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注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标平台。- 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注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标平台。- //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注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标平台。- //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注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标平台。- 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注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标平台。- 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 参数,然后将其值设置为