شما می توانید از Firebase Remote Config برای تعریف پارامترها در برنامه خود و به روز رسانی مقادیر آنها در فضای ابری استفاده کنید و به شما امکان می دهد ظاهر و رفتار برنامه خود را بدون توزیع به روز رسانی برنامه تغییر دهید. این راهنما شما را در مراحل شروع به کار راهنمایی می کند و نمونه کدی را ارائه می دهد که همگی برای شبیه سازی یا دانلود از مخزن firebase/quickstart-ios GitHub در دسترس هستند.
مرحله 1: Remote Config را به برنامه خود اضافه کنید
همانطور که در مثال زیر نشان داده شده است، شی singleton Remote Config را ایجاد کنید:
سریع
remoteConfig = RemoteConfig.remoteConfig() let settings = RemoteConfigSettings() settings.minimumFetchInterval = 0 remoteConfig.configSettings = settings
هدف-C
self.remoteConfig = [FIRRemoteConfig remoteConfig]; FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] init]; remoteConfigSettings.minimumFetchInterval = 0; self.remoteConfig.configSettings = remoteConfigSettings;
این شیء برای ذخیره مقادیر پارامترهای پیشفرض درون برنامه، واکشی مقادیر پارامترهای بهروزشده از باطن Remote Config و کنترل زمانی که مقادیر واکشی شده در دسترس برنامه شما قرار میگیرند، استفاده میشود.
در طول توسعه، توصیه می شود حداقل فاصله واکشی نسبتاً کم را تنظیم کنید. برای اطلاعات بیشتر Throttling را ببینید.
مرحله 2: مقادیر پارامترهای پیش فرض درون برنامه را تنظیم کنید
میتوانید مقادیر پارامترهای پیشفرض درون برنامهای را در شی Remote Config تنظیم کنید، به طوری که برنامه شما قبل از اتصال به پشتیبان Remote Config همانطور که میخواهید رفتار کند، و اگر مقادیر پیشفرض در backend تنظیم نشده باشد، در دسترس باشند.
مجموعه ای از نام پارامترها و مقادیر پارامترهای پیش فرض را با استفاده از یک شی
NSDictionary
یا یک فایل plist تعریف کنید.اگر قبلاً مقادیر پارامتر Backend Config Remote را پیکربندی کردهاید، میتوانید یک فایل
plist
تولید شده را دانلود کنید که شامل تمام مقادیر پیشفرض است و آن را در پروژه Xcode خود ذخیره کنید.باقی مانده
curl --compressed -D headers -H "Authorization: Bearer token -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig:downloadDefaults?format=PLIST -o RemoteConfigDefaults.plist
کنسول Firebase
در برگه پارامترها ، منوی را باز کنید و مقادیر پیشفرض را دانلود کنید .
وقتی از شما خواسته شد، plist. را برای iOS فعال کنید، سپس روی دانلود فایل کلیک کنید.
این مقادیر را با استفاده از
setDefaults:
. مثال زیر مقادیر پیشفرض درون برنامهای را از یک فایل plist تنظیم میکند:سریع
remoteConfig.setDefaults(fromPlist: "RemoteConfigDefaults")
هدف-C
[self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"];
Step 3: Get parameter values to use in your app
Now you can get parameter values from the Remote Config object. If you later set values in the Remote Config backend, fetch them, and then activate them, those values are available to your app. Otherwise, you get the in-app parameter values configured using setDefaults:
. To get these values, call the configValueForKey:
method, providing the parameter key as an argument.
Step 4: Set parameter values
Using the Firebase console or the Remote Config backend APIs , you can create new backend default values that override the in-app values according to your desired conditional logic or user targeting. This section walks you through the Firebase console steps to create these values.
- در کنسول Firebase ، پروژه خود را باز کنید.
- Select Remote Config from the menu to view the Remote Config dashboard.
- Define parameters with the same names as the parameters that you defined in your app. For each parameter, you can set a default value (which will eventually override the in-app default value) and you can also set conditional values. To learn more, see Remote Config Parameters and Conditions .
Step 5: Fetch and activate values
To fetch parameter values from Remote Config, call the fetchWithCompletionHandler:
or fetchWithExpirationDuration:completionHandler:
method. Any values that you set on the backend are fetched and cached in the Remote Config object.
For cases where you want to fetch and activate values in one call, use fetchAndActivateWithCompletionHandler:
.
This example fetches values from the Remote Config backend (not cached values) and calls activateWithCompletionHandler:
to make them available to the app:
سریع
remoteConfig.fetch { (status, error) -> Void in if status == .success { print("Config fetched!") self.remoteConfig.activate { changed, error in // ... } } else { print("Config not fetched") print("Error: \(error?.localizedDescription ?? "No error available.")") } self.displayWelcome() }
هدف-C
[self.remoteConfig fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) { if (status == FIRRemoteConfigFetchStatusSuccess) { NSLog(@"Config fetched!"); [self.remoteConfig activateWithCompletion:^(BOOL changed, NSError * _Nullable error) { if (error != nil) { NSLog(@"Activate error: %@", error.localizedDescription); } else { dispatch_async(dispatch_get_main_queue(), ^{ [self displayWelcome]; }); } }]; } else { NSLog(@"Config not fetched"); NSLog(@"Error %@", error.localizedDescription); } }];
Because these updated parameter values affect the behavior and appearance of your app, you should activate the fetched values at a time that ensures a smooth experience for your user, such as the next time that the user opens your app. See Remote Config loading strategies for more information and examples.
Step 6: Listen for updates in real time
After you fetch parameter values, you can use real-time Remote Config to listen for updates from the Remote Config backend. Real-time Remote Config signals to connected devices when updates are available and automatically fetches the changes after you publish a new Remote Config version.
Real-time updates are supported by the Firebase SDK for Apple platforms v10.7.0+ and higher.
In your app, call
addOnConfigUpdateListener
to start listening for updates and automatically fetch any new or updated parameter values. The following example listens for updates and whenactivateWithCompletionHandler
is called, uses the newly fetched values to display an updated welcome message.سریع
remoteConfig.addOnConfigUpdateListener { configUpdate, error in guard let configUpdate, error == nil else { print("Error listening for config updates: \(error)") } print("Updated keys: \(configUpdate.updatedKeys)") self.remoteConfig.activate { changed, error in guard error == nil else { return self.displayError(error) } DispatchQueue.main.async { self.displayWelcome() } } }
هدف-C
__weak __typeof__(self) weakSelf = self; [self.remoteConfig addOnConfigUpdateListener:^(FIRRemoteConfigUpdate * _Nonnull configUpdate, NSError * _Nullable error) { if (error != nil) { NSLog(@"Error listening for config updates %@", error.localizedDescription); } else { NSLog(@"Updated keys: %@", configUpdate.updatedKeys); __typeof__(self) strongSelf = weakSelf; [strongSelf.remoteConfig activateWithCompletion:^(BOOL changed, NSError * _Nullable error) { if (error != nil) { NSLog(@"Activate error %@", error.localizedDescription); } dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf displayWelcome]; }); }]; } }];
The next time you publish a new version of your Remote Config, devices that are running your app and listening for changes will call the completion handler.
Throttling
If an app fetches too many times in a short time period, fetch calls are throttled and the SDK returns FIRRemoteConfigFetchStatusThrottled
. Before SDK version 6.3.0, the limit was 5 fetch requests in a 60 minute window (newer versions have more permissive limits).
During app development,you might want to fetch more often to refresh the cache very frequently (many times per hour) to let you rapidly iterate as you develop and test your app. Real-time Remote Config updates automatically bypass the cache when the config is updated on the server. To accommodate rapid iteration on a project with numerous developers, you can temporarily add a FIRRemoteConfigSettings
property with a low minimum fetch interval ( MinimumFetchInterval
) in your app.
The default and recommended production fetch interval for Remote Config is 12 hours, which means that configs won't be fetched from the backend more than once in a 12 hour window, regardless of how many fetch calls are actually made. Specifically, the minimum fetch interval is determined in this following order:
- The parameter in
fetch(long)
- The parameter in
FIRRemoteConfigSettings.MinimumFetchInterval
- The default value of 12 hours
مراحل بعدی
If you haven't already, explore the Remote Config use cases , and take a look at some of the key concepts and advanced strategies documentation, including: