要让您的用户选择加入或退出使用 Firebase 性能监控,您可能需要配置您的应用,以便您可以启用和禁用性能监控。您可能还会发现此功能在应用程序开发和测试期间很有用。
以下是一些需要考虑的选项:
您可以在构建应用程序时禁用性能监控 SDK,并可选择在运行时重新启用它。
您可以在启用性能监控 SDK 的情况下构建您的应用程序,但可以选择在运行时使用 Firebase 远程配置将其禁用。
您可以完全停用 Performance Monitoring SDK,没有在运行时启用它的选项。
在您的应用构建过程中禁用性能监控
在应用构建过程中禁用性能监控可能有用的一种情况是,避免在应用开发和测试期间报告应用预发布版本的性能数据。
要禁用或停用性能监控,您可以将两个键之一添加到您的 Apple 应用程序的属性列表文件 ( Info.plist
):
要禁用性能监控,但允许您的应用在运行时启用它,请在应用的
Info.plist
文件中将firebase_performance_collection_enabled
设置为false
。要完全停用性能监控,并且没有在运行时启用它的选项,请在应用程序的
Info.plist
文件中将firebase_performance_collection_deactivated
设置为true
。
使用远程配置在运行时禁用您的应用
Firebase Remote Config 允许您更改应用程序的行为和外观,因此它提供了一种理想的方式让您在已部署的应用程序实例中禁用性能监控。
要在您的 Apple 应用程序下次启动时禁用性能监控数据收集,请使用下面显示的示例代码。有关在 Apple 应用程序中使用 Remote Config 的更多信息,请参阅在 Apple 平台上使用 Firebase Remote Config 。
确保您的
Podfile
中使用了远程配置:pod 'Firebase/RemoteConfig'
将以下内容添加到应用的
AppDelegate
文件的顶部:迅速
注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标。import FirebaseRemoteConfig
目标-C
注意:此 Firebase 产品不适用于 macOS、Mac Catalyst、watchOS 目标。@import FirebaseRemoteConfig;
在您的
AppDelegate
文件中,将以下代码添加到application:didFinishLaunchingWithOptions:
实例方法中的launchOptions
语句中:迅速
注意:此产品不适用于 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()
目标-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
或您的应用程序使用的另一个实现文件中,添加以下代码以获取和激活远程配置值:迅速
注意:此 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)") } }
目标-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 控制台中禁用性能监控,请在您的应用项目中创建一个perf_disable参数,然后将其值设置为
true
。如果将perf_disable的值设置为
false
,性能监控将保持启用状态。
单独禁用自动或自定义数据收集
您可以对上面显示的代码和 Firebase 控制台中的代码进行一些更改,以让您禁用所有自动(开箱即用)监控,与自定义监控分开。
将以下代码添加到
application:didFinishLaunchingWithOptions:
实例方法中的launchOptions
语句(而不是上面显示的同一实例方法):迅速
注意:此 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()
目标-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参数,然后将其值设置为