FIRRemoteConfig.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. @class FIRApp;
  18. /// The Firebase Remote Config service default namespace, to be used if the API method does not
  19. /// specify a different namespace. Use the default namespace if configuring from the Google Firebase
  20. /// service.
  21. extern NSString *const _Nonnull FIRNamespaceGoogleMobilePlatform NS_SWIFT_NAME(
  22. NamespaceGoogleMobilePlatform);
  23. /// Key used to manage throttling in NSError user info when the refreshing of Remote Config
  24. /// parameter values (data) is throttled. The value of this key is the elapsed time since 1970,
  25. /// measured in seconds.
  26. extern NSString *const _Nonnull FIRRemoteConfigThrottledEndTimeInSecondsKey NS_SWIFT_NAME(
  27. RemoteConfigThrottledEndTimeInSecondsKey);
  28. /// Indicates whether updated data was successfully fetched.
  29. typedef NS_ENUM(NSInteger, FIRRemoteConfigFetchStatus) {
  30. /// Config has never been fetched.
  31. FIRRemoteConfigFetchStatusNoFetchYet,
  32. /// Config fetch succeeded.
  33. FIRRemoteConfigFetchStatusSuccess,
  34. /// Config fetch failed.
  35. FIRRemoteConfigFetchStatusFailure,
  36. /// Config fetch was throttled.
  37. FIRRemoteConfigFetchStatusThrottled,
  38. } NS_SWIFT_NAME(RemoteConfigFetchStatus);
  39. /// Indicates whether updated data was successfully fetched and activated.
  40. typedef NS_ENUM(NSInteger, FIRRemoteConfigFetchAndActivateStatus) {
  41. /// The remote fetch succeeded and fetched data was activated.
  42. FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote,
  43. /// The fetch and activate succeeded from already fetched but yet unexpired config data. You can
  44. /// control this using minimumFetchInterval property in FIRRemoteConfigSettings.
  45. FIRRemoteConfigFetchAndActivateStatusSuccessUsingPreFetchedData,
  46. /// The fetch and activate failed.
  47. FIRRemoteConfigFetchAndActivateStatusError
  48. } NS_SWIFT_NAME(RemoteConfigFetchAndActivateStatus);
  49. /// Remote Config error domain that handles errors when fetching data from the service.
  50. extern NSString *const _Nonnull FIRRemoteConfigErrorDomain NS_SWIFT_NAME(RemoteConfigErrorDomain);
  51. /// Firebase Remote Config service fetch error.
  52. typedef NS_ENUM(NSInteger, FIRRemoteConfigError) {
  53. /// Unknown or no error.
  54. FIRRemoteConfigErrorUnknown = 8001,
  55. /// Frequency of fetch requests exceeds throttled limit.
  56. FIRRemoteConfigErrorThrottled = 8002,
  57. /// Internal error that covers all internal HTTP errors.
  58. FIRRemoteConfigErrorInternalError = 8003,
  59. } NS_SWIFT_NAME(RemoteConfigError);
  60. /// Enumerated value that indicates the source of Remote Config data. Data can come from
  61. /// the Remote Config service, the DefaultConfig that is available when the app is first installed,
  62. /// or a static initialized value if data is not available from the service or DefaultConfig.
  63. typedef NS_ENUM(NSInteger, FIRRemoteConfigSource) {
  64. FIRRemoteConfigSourceRemote, ///< The data source is the Remote Config service.
  65. FIRRemoteConfigSourceDefault, ///< The data source is the DefaultConfig defined for this app.
  66. FIRRemoteConfigSourceStatic, ///< The data doesn't exist, return a static initialized value.
  67. } NS_SWIFT_NAME(RemoteConfigSource);
  68. /// Completion handler invoked by fetch methods when they get a response from the server.
  69. ///
  70. /// @param status Config fetching status.
  71. /// @param error Error message on failure.
  72. typedef void (^FIRRemoteConfigFetchCompletion)(FIRRemoteConfigFetchStatus status,
  73. NSError *_Nullable error)
  74. NS_SWIFT_NAME(RemoteConfigFetchCompletion);
  75. /// Completion handler invoked by activate method upon completion.
  76. /// @param error Error message on failure. Nil if activation was successful.
  77. typedef void (^FIRRemoteConfigActivateCompletion)(NSError *_Nullable error)
  78. NS_SWIFT_NAME(RemoteConfigActivateCompletion);
  79. /// Completion handler invoked upon completion of Remote Config initialization.
  80. ///
  81. /// @param initializationError nil if initialization succeeded.
  82. typedef void (^FIRRemoteConfigInitializationCompletion)(NSError *_Nullable initializationError)
  83. NS_SWIFT_NAME(RemoteConfigInitializationCompletion);
  84. /// Completion handler invoked by the fetchAndActivate method. Used to convey status of fetch and,
  85. /// if successful, resultant activate call
  86. /// @param status Config fetching status.
  87. /// @param error Error message on failure of config fetch
  88. typedef void (^FIRRemoteConfigFetchAndActivateCompletion)(
  89. FIRRemoteConfigFetchAndActivateStatus status, NSError *_Nullable error)
  90. NS_SWIFT_NAME(RemoteConfigFetchAndActivateCompletion);
  91. #pragma mark - FIRRemoteConfigValue
  92. /// This class provides a wrapper for Remote Config parameter values, with methods to get parameter
  93. /// values as different data types.
  94. NS_SWIFT_NAME(RemoteConfigValue)
  95. @interface FIRRemoteConfigValue : NSObject <NSCopying>
  96. /// Gets the value as a string.
  97. @property(nonatomic, readonly, nullable) NSString *stringValue;
  98. /// Gets the value as a number value.
  99. @property(nonatomic, readonly, nullable) NSNumber *numberValue;
  100. /// Gets the value as a NSData object.
  101. @property(nonatomic, readonly, nonnull) NSData *dataValue;
  102. /// Gets the value as a boolean.
  103. @property(nonatomic, readonly) BOOL boolValue;
  104. /// Gets a foundation object (NSDictionary / NSArray) by parsing the value as JSON. This method uses
  105. /// NSJSONSerialization's JSONObjectWithData method with an options value of 0.
  106. @property(nonatomic, readonly, nullable) id JSONValue NS_SWIFT_NAME(jsonValue);
  107. /// Identifies the source of the fetched value.
  108. @property(nonatomic, readonly) FIRRemoteConfigSource source;
  109. @end
  110. #pragma mark - FIRRemoteConfigSettings
  111. /// Firebase Remote Config settings.
  112. NS_SWIFT_NAME(RemoteConfigSettings)
  113. @interface FIRRemoteConfigSettings : NSObject
  114. /// Indicates the default value in seconds to set for the minimum interval that needs to elapse
  115. /// before a fetch request can again be made to the Remote Config backend. After a fetch request to
  116. /// the backend has succeeded, no additional fetch requests to the backend will be allowed until the
  117. /// minimum fetch interval expires. Note that you can override this default on a per-fetch request
  118. /// basis using -[FIRRemoteConfig fetchWithExpirationDuration:completionHandler]. For E.g. setting
  119. /// the expiration duration to 0 in the fetch request will override the minimumFetchInterval and
  120. /// allow the request to the backend.
  121. @property(nonatomic, assign) NSTimeInterval minimumFetchInterval;
  122. /// Indicates the default value in seconds to abandon a pending fetch request made to the backend.
  123. /// This value is set for outgoing requests as the timeoutIntervalForRequest as well as the
  124. /// timeoutIntervalForResource on the NSURLSession's configuration.
  125. @property(nonatomic, assign) NSTimeInterval fetchTimeout;
  126. /// Indicates whether Developer Mode is enabled.
  127. @property(nonatomic, readonly) BOOL isDeveloperModeEnabled DEPRECATED_MSG_ATTRIBUTE(
  128. "This no longer needs to be set during development. Refer to documentation for additional "
  129. "details.");
  130. /// Initializes FIRRemoteConfigSettings, which is used to set properties for custom settings. To
  131. /// make custom settings take effect, pass the FIRRemoteConfigSettings instance to the
  132. /// configSettings property of FIRRemoteConfig.
  133. - (nonnull FIRRemoteConfigSettings *)initWithDeveloperModeEnabled:(BOOL)developerModeEnabled
  134. DEPRECATED_MSG_ATTRIBUTE("This no longer needs to be set during development. Refer to "
  135. "documentation for additional details.");
  136. @end
  137. #pragma mark - FIRRemoteConfig
  138. /// Firebase Remote Config class. The shared instance method +remoteConfig can be created and used
  139. /// to fetch, activate and read config results and set default config results.
  140. NS_SWIFT_NAME(RemoteConfig)
  141. @interface FIRRemoteConfig : NSObject <NSFastEnumeration>
  142. /// Last successful fetch completion time.
  143. @property(nonatomic, readwrite, strong, nullable) NSDate *lastFetchTime;
  144. /// Last fetch status. The status can be any enumerated value from FIRRemoteConfigFetchStatus.
  145. @property(nonatomic, readonly, assign) FIRRemoteConfigFetchStatus lastFetchStatus;
  146. /// Config settings are custom settings.
  147. @property(nonatomic, readwrite, strong, nonnull) FIRRemoteConfigSettings *configSettings;
  148. /// Returns the FIRRemoteConfig instance configured for the default Firebase app. This singleton
  149. /// object contains the complete set of Remote Config parameter values available to the app,
  150. /// including the Active Config and Default Config. This object also caches values fetched from the
  151. /// Remote Config Server until they are copied to the Active Config by calling activateFetched. When
  152. /// you fetch values from the Remote Config Server using the default Firebase namespace service, you
  153. /// should use this class method to create a shared instance of the FIRRemoteConfig object to ensure
  154. /// that your app will function properly with the Remote Config Server and the Firebase service.
  155. + (nonnull FIRRemoteConfig *)remoteConfig NS_SWIFT_NAME(remoteConfig());
  156. /// Returns the FIRRemoteConfig instance for your (non-default) Firebase appID. Note that Firebase
  157. /// analytics does not work for non-default app instances. This singleton object contains the
  158. /// complete set of Remote Config parameter values available to the app, including the Active Config
  159. /// and Default Config. This object also caches values fetched from the Remote Config Server until
  160. /// they are copied to the Active Config by calling activateFetched. When you fetch values from the
  161. /// Remote Config Server using the default Firebase namespace service, you should use this class
  162. /// method to create a shared instance of the FIRRemoteConfig object to ensure that your app will
  163. /// function properly with the Remote Config Server and the Firebase service.
  164. + (nonnull FIRRemoteConfig *)remoteConfigWithApp:(nonnull FIRApp *)app
  165. NS_SWIFT_NAME(remoteConfig(app:));
  166. /// Unavailable. Use +remoteConfig instead.
  167. - (nonnull instancetype)init __attribute__((unavailable("Use +remoteConfig instead.")));
  168. /// Ensures initialization is complete and clients can begin querying for Remote Config values.
  169. /// @param completionHandler Initialization complete callback.
  170. - (void)ensureInitializedWithCompletionHandler:
  171. (nonnull FIRRemoteConfigInitializationCompletion)completionHandler;
  172. #pragma mark - Fetch
  173. /// Fetches Remote Config data with a callback. Call activateFetched to make fetched data available
  174. /// to your app.
  175. ///
  176. /// Note: This method uses a Firebase Instance ID token to identify the app instance, and once it's
  177. /// called, it periodically sends data to the Firebase backend. (see
  178. /// `[FIRInstanceID getIDWithHandler:]`).
  179. /// To stop the periodic sync, developers need to call `[FIRInstanceID deleteIDWithHandler:]` and
  180. /// avoid calling this method again.
  181. ///
  182. /// @param completionHandler Fetch operation callback.
  183. - (void)fetchWithCompletionHandler:(nullable FIRRemoteConfigFetchCompletion)completionHandler;
  184. /// Fetches Remote Config data and sets a duration that specifies how long config data lasts.
  185. /// Call activateFetched to make fetched data available to your app.
  186. ///
  187. /// Note: This method uses a Firebase Instance ID token to identify the app instance, and once it's
  188. /// called, it periodically sends data to the Firebase backend. (see
  189. /// `[FIRInstanceID getIDWithHandler:]`).
  190. /// To stop the periodic sync, developers need to call `[FIRInstanceID deleteIDWithHandler:]` and
  191. /// avoid calling this method again.
  192. ///
  193. /// @param expirationDuration Override the (default or optionally set minimumFetchInterval property
  194. /// in FIRRemoteConfigSettings) minimumFetchInterval for only the current request, in seconds.
  195. /// Setting a value of 0 seconds will force a fetch to the backend.
  196. /// @param completionHandler Fetch operation callback.
  197. - (void)fetchWithExpirationDuration:(NSTimeInterval)expirationDuration
  198. completionHandler:(nullable FIRRemoteConfigFetchCompletion)completionHandler;
  199. /// Fetches Remote Config data and if successful, activates fetched data. Optional completion
  200. /// handler callback is invoked after the attempted activation of data, if the fetch call succeeded.
  201. ///
  202. /// Note: This method uses a Firebase Instance ID token to identify the app instance, and once it's
  203. /// called, it periodically sends data to the Firebase backend. (see
  204. /// `[FIRInstanceID getIDWithHandler:]`).
  205. /// To stop the periodic sync, developers need to call `[FIRInstanceID deleteIDWithHandler:]` and
  206. /// avoid calling this method again.
  207. ///
  208. /// @param completionHandler Fetch operation callback.
  209. - (void)fetchAndActivateWithCompletionHandler:
  210. (nullable FIRRemoteConfigFetchAndActivateCompletion)completionHandler;
  211. #pragma mark - Apply
  212. /// Applies Fetched Config data to the Active Config, causing updates to the behavior and appearance
  213. /// of the app to take effect (depending on how config data is used in the app).
  214. /// @param completionHandler Activate operation callback.
  215. - (void)activateWithCompletionHandler:(nullable FIRRemoteConfigActivateCompletion)completionHandler;
  216. /// This method is deprecated. Please use -[FIRRemoteConfig activateWithCompletionHandler:] instead.
  217. /// Applies Fetched Config data to the Active Config, causing updates to the behavior and appearance
  218. /// of the app to take effect (depending on how config data is used in the app).
  219. /// Returns true if there was a Fetched Config, and it was activated.
  220. /// Returns false if no Fetched Config was found, or the Fetched Config was already activated.
  221. - (BOOL)activateFetched DEPRECATED_MSG_ATTRIBUTE("Use -[FIRRemoteConfig activate] "
  222. "instead.");
  223. #pragma mark - Get Config
  224. /// Enables access to configuration values by using object subscripting syntax.
  225. /// <pre>
  226. /// // Example:
  227. /// FIRRemoteConfig *config = [FIRRemoteConfig remoteConfig];
  228. /// FIRRemoteConfigValue *value = config[@"yourKey"];
  229. /// BOOL b = value.boolValue;
  230. /// NSNumber *number = config[@"yourKey"].numberValue;
  231. /// </pre>
  232. - (nonnull FIRRemoteConfigValue *)objectForKeyedSubscript:(nonnull NSString *)key;
  233. /// Gets the config value.
  234. /// @param key Config key.
  235. - (nonnull FIRRemoteConfigValue *)configValueForKey:(nullable NSString *)key;
  236. /// Gets the config value of a given namespace.
  237. /// @param key Config key.
  238. /// @param aNamespace Config results under a given namespace.
  239. - (nonnull FIRRemoteConfigValue *)configValueForKey:(nullable NSString *)key
  240. namespace:(nullable NSString *)aNamespace
  241. DEPRECATED_MSG_ATTRIBUTE("Use -[FIRRemoteConfig configValueForKey:] "
  242. "instead.");
  243. /// Gets the config value of a given namespace and a given source.
  244. /// @param key Config key.
  245. /// @param source Config value source.
  246. - (nonnull FIRRemoteConfigValue *)configValueForKey:(nullable NSString *)key
  247. source:(FIRRemoteConfigSource)source;
  248. /// Gets the config value of a given namespace and a given source.
  249. /// @param key Config key.
  250. /// @param aNamespace Config results under a given namespace.
  251. /// @param source Config value source.
  252. - (nonnull FIRRemoteConfigValue *)configValueForKey:(nullable NSString *)key
  253. namespace:(nullable NSString *)aNamespace
  254. source:(FIRRemoteConfigSource)source
  255. DEPRECATED_MSG_ATTRIBUTE("Use -[FIRRemoteConfig configValueForKey:source:] "
  256. "instead.");
  257. /// Gets all the parameter keys from a given source and a given namespace.
  258. ///
  259. /// @param source The config data source.
  260. /// @return An array of keys under the given source and namespace.
  261. - (nonnull NSArray<NSString *> *)allKeysFromSource:(FIRRemoteConfigSource)source;
  262. /// Gets all the parameter keys from a given source and a given namespace.
  263. ///
  264. /// @param source The config data source.
  265. /// @param aNamespace The config data namespace.
  266. /// @return An array of keys under the given source and namespace.
  267. - (nonnull NSArray<NSString *> *)allKeysFromSource:(FIRRemoteConfigSource)source
  268. namespace:(nullable NSString *)aNamespace
  269. DEPRECATED_MSG_ATTRIBUTE("Use -[FIRRemoteConfig allKeysFromSource:] instead.");
  270. /// Returns the set of parameter keys that start with the given prefix, from the default namespace
  271. /// in the active config.
  272. ///
  273. /// @param prefix The key prefix to look for. If prefix is nil or empty, returns all the
  274. /// keys.
  275. /// @return The set of parameter keys that start with the specified prefix.
  276. - (nonnull NSSet<NSString *> *)keysWithPrefix:(nullable NSString *)prefix;
  277. /// Returns the set of parameter keys that start with the given prefix, from the given namespace in
  278. /// the active config.
  279. ///
  280. /// @param prefix The key prefix to look for. If prefix is nil or empty, returns all the
  281. /// keys in the given namespace.
  282. /// @param aNamespace The namespace in which to look up the keys. If the namespace is invalid,
  283. /// returns an empty set.
  284. /// @return The set of parameter keys that start with the specified prefix.
  285. - (nonnull NSSet<NSString *> *)keysWithPrefix:(nullable NSString *)prefix
  286. namespace:(nullable NSString *)aNamespace
  287. DEPRECATED_MSG_ATTRIBUTE("Use -[FIRRemoteConfig keysWithPrefix:] instead.");
  288. #pragma mark - Defaults
  289. /// Sets config defaults for parameter keys and values in the default namespace config.
  290. /// @param defaults A dictionary mapping a NSString * key to a NSObject * value.
  291. - (void)setDefaults:(nullable NSDictionary<NSString *, NSObject *> *)defaults;
  292. /// Sets config defaults for parameter keys and values in the default namespace config.
  293. ///
  294. /// @param defaults A dictionary mapping a NSString * key to a NSObject * value.
  295. /// @param aNamespace Config under a given namespace.
  296. - (void)setDefaults:(nullable NSDictionary<NSString *, NSObject *> *)defaults
  297. namespace:(nullable NSString *)aNamespace
  298. DEPRECATED_MSG_ATTRIBUTE("Use -[FIRRemoteConfig setDefaults:] instead.");
  299. /// Sets default configs from plist for default namespace;
  300. /// @param fileName The plist file name, with no file name extension. For example, if the plist file
  301. /// is defaultSamples.plist, call:
  302. /// [[FIRRemoteConfig remoteConfig] setDefaultsFromPlistFileName:@"defaultSamples"];
  303. - (void)setDefaultsFromPlistFileName:(nullable NSString *)fileName
  304. NS_SWIFT_NAME(setDefaults(fromPlist:));
  305. /// Sets default configs from plist for a given namespace;
  306. /// @param fileName The plist file name, with no file name extension. For example, if the plist file
  307. /// is defaultSamples.plist, call:
  308. /// [[FIRRemoteConfig remoteConfig] setDefaultsFromPlistFileName:@"defaultSamples"];
  309. /// @param aNamespace The namespace where the default config is set.
  310. - (void)setDefaultsFromPlistFileName:(nullable NSString *)fileName
  311. namespace:(nullable NSString *)aNamespace
  312. NS_SWIFT_NAME(setDefaults(fromPlist:namespace:))
  313. DEPRECATED_MSG_ATTRIBUTE("Use -[FIRRemoteConfig setDefaultsFromPlistFileName:] instead.");
  314. /// Returns the default value of a given key and a given namespace from the default config.
  315. ///
  316. /// @param key The parameter key of default config.
  317. /// @return Returns the default value of the specified key and namespace. Returns
  318. /// nil if the key or namespace doesn't exist in the default config.
  319. - (nullable FIRRemoteConfigValue *)defaultValueForKey:(nullable NSString *)key;
  320. /// Returns the default value of a given key and a given namespace from the default config.
  321. ///
  322. /// @param key The parameter key of default config.
  323. /// @param aNamespace The namespace of default config.
  324. /// @return Returns the default value of the specified key and namespace. Returns
  325. /// nil if the key or namespace doesn't exist in the default config.
  326. - (nullable FIRRemoteConfigValue *)defaultValueForKey:(nullable NSString *)key
  327. namespace:(nullable NSString *)aNamespace
  328. DEPRECATED_MSG_ATTRIBUTE("Use -[FIRRemoteConfig defaultValueForKey:] instead.");
  329. @end