ViewController.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "ViewController.h"
  15. #import <FirebaseAnalytics/FirebaseAnalytics.h>
  16. #import <FirebaseCore/FIROptions.h>
  17. #import <FirebaseCore/FirebaseCore.h>
  18. #import <FirebaseInstallations/FirebaseInstallations.h>
  19. #import <FirebaseRemoteConfig/FirebaseRemoteConfig.h>
  20. #import "../../../Sources/Private/FIRRemoteConfig_Private.h"
  21. #import "FRCLog.h"
  22. static NSString *const FIRPerfNamespace = @"fireperf";
  23. static NSString *const FIRDefaultFIRAppName = @"__FIRAPP_DEFAULT";
  24. static NSString *const FIRSecondFIRAppName = @"secondFIRApp";
  25. @interface FIRRemoteConfig (Sample)
  26. + (FIRRemoteConfig *)remoteConfigWithFIRNamespace:(NSString *)remoteConfigNamespace
  27. app:(FIRApp *)app;
  28. @end
  29. @interface ViewController ()
  30. @property(nonatomic, strong) IBOutlet UIButton *fetchButton;
  31. @property(nonatomic, strong) IBOutlet UIButton *applyButton;
  32. @property(nonatomic, strong) IBOutlet UIButton *refreshButton;
  33. @property(nonatomic, strong) IBOutlet UIButton *clearLogsButton;
  34. @property(nonatomic, strong) IBOutlet UITextView *mainTextView;
  35. /// Key of custom variable to be added by user.
  36. @property(nonatomic, weak) IBOutlet UITextField *keyLabel;
  37. /// Value of custom variable to be added by user.
  38. @property(nonatomic, weak) IBOutlet UITextField *valueLabel;
  39. /// Expiration in seconds to be set by user.
  40. @property(nonatomic, weak) IBOutlet UITextField *expirationLabel;
  41. /// Config Defaults.
  42. @property(nonatomic, strong) NSMutableDictionary *configDefaults;
  43. /// developer mode switch.
  44. @property(strong, nonatomic) IBOutlet UISwitch *developerModeEnabled;
  45. /// Current selected namespace.
  46. @property(nonatomic, copy) NSString *currentNamespace;
  47. /// Curret selected FIRApp instance name.
  48. @property(nonatomic, copy) NSString *FIRAppName;
  49. /// Selected namespace picker control view.
  50. @property(nonatomic, strong) IBOutlet UIPickerView *namespacePicker;
  51. /// Selected app picker control view.
  52. @property(nonatomic, strong) IBOutlet UIPickerView *appPicker;
  53. /// Array of prepopulated namespaces supported by this app.
  54. @property(nonatomic, strong) NSArray<NSString *> *namespacePickerData;
  55. /// Array of prepopulated FIRApp names supported by this app.
  56. @property(nonatomic, strong) NSArray<NSString *> *appPickerData;
  57. /// Array of Remote Config instances.
  58. @property(nonatomic, strong) NSMutableDictionary *RCInstances;
  59. @end
  60. @implementation ViewController
  61. - (void)viewDidLoad {
  62. [super viewDidLoad];
  63. [[FRCLog sharedInstance] setLogView:self.mainTextView];
  64. [[FRCLog sharedInstance] logToConsole:@"Viewcontroller loaded"];
  65. [[FRCLog sharedInstance] logToConsole:[[NSBundle mainBundle] bundleIdentifier]];
  66. // Setup UI
  67. self.expirationLabel.text = [NSString stringWithFormat:@"0"];
  68. self.configDefaults = [[NSMutableDictionary alloc] init];
  69. self.keyLabel.delegate = self;
  70. self.valueLabel.delegate = self;
  71. self.expirationLabel.delegate = self;
  72. self.mainTextView.editable = NO;
  73. // TODO(mandard): Add support for deleting and adding namespaces in the app.
  74. self.namespacePickerData =
  75. [[NSArray alloc] initWithObjects:FIRNamespaceGoogleMobilePlatform, FIRPerfNamespace, nil];
  76. self.appPickerData =
  77. [[NSArray alloc] initWithObjects:FIRDefaultFIRAppName, FIRSecondFIRAppName, nil];
  78. self.RCInstances = [[NSMutableDictionary alloc] init];
  79. for (NSString *namespaceString in self.namespacePickerData) {
  80. for (NSString *appString in self.appPickerData) {
  81. // Check for the default instance.
  82. if (!self.RCInstances[namespaceString]) {
  83. self.RCInstances[namespaceString] = [[NSMutableDictionary alloc] init];
  84. }
  85. if ([namespaceString isEqualToString:FIRNamespaceGoogleMobilePlatform] &&
  86. [appString isEqualToString:FIRDefaultFIRAppName]) {
  87. self.RCInstances[namespaceString][appString] = [FIRRemoteConfig remoteConfig];
  88. } else {
  89. FIRApp *firebaseApp = ([appString isEqualToString:FIRDefaultFIRAppName])
  90. ? [FIRApp defaultApp]
  91. : [FIRApp appNamed:appString];
  92. self.RCInstances[namespaceString][appString] =
  93. [FIRRemoteConfig remoteConfigWithFIRNamespace:namespaceString app:firebaseApp];
  94. }
  95. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  96. settings.fetchTimeout = 300;
  97. settings.minimumFetchInterval = 0;
  98. ((FIRRemoteConfig *)(self.RCInstances[namespaceString][appString])).configSettings = settings;
  99. }
  100. }
  101. [[FRCLog sharedInstance] logToConsole:@"RC instances inited"];
  102. self.namespacePicker.dataSource = self;
  103. self.namespacePicker.delegate = self;
  104. self.appPicker.dataSource = self;
  105. self.appPicker.delegate = self;
  106. [self.developerModeEnabled setOn:true animated:false];
  107. }
  108. - (IBAction)fetchButtonPressed:(id)sender {
  109. [[FRCLog sharedInstance] logToConsole:@"Fetch button pressed"];
  110. // fetchConfig api callback, this is triggered when client receives response from server
  111. ViewController *__weak weakSelf = self;
  112. FIRRemoteConfigFetchCompletion completion = ^(FIRRemoteConfigFetchStatus status, NSError *error) {
  113. ViewController *strongSelf = weakSelf;
  114. if (!strongSelf) {
  115. return;
  116. }
  117. [[FRCLog sharedInstance]
  118. logToConsole:[NSString stringWithFormat:@"Fetch completed. Status=%@",
  119. [strongSelf statusString:status]]];
  120. if (error) {
  121. [[FRCLog sharedInstance] logToConsole:[NSString stringWithFormat:@"Fetch Error=%@", error]];
  122. }
  123. NSMutableString *output = [NSMutableString
  124. stringWithFormat:@"Fetch status : %@.\n\n", [strongSelf statusString:status]];
  125. if (error) {
  126. [output appendFormat:@"%@\n", error];
  127. }
  128. if (status == FIRRemoteConfigFetchStatusFailure) {
  129. [output appendString:[NSString stringWithFormat:@"Fetch Error :%@.\n",
  130. [strongSelf errorString:error.code]]];
  131. if (error.code == FIRRemoteConfigErrorThrottled) {
  132. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  133. NSNumber *throttledTime =
  134. (NSNumber *)error.userInfo[FIRRemoteConfigThrottledEndTimeInSecondsKey];
  135. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[throttledTime doubleValue]];
  136. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  137. NSString *timeString = [dateFormatter stringFromDate:date];
  138. [output appendString:[NSString stringWithFormat:@"Throttled end at: %@ \n", timeString]];
  139. }
  140. }
  141. [[FRCLog sharedInstance] logToConsole:output];
  142. };
  143. // fetchConfig api call
  144. [[FRCLog sharedInstance] logToConsole:@"Calling fetchWithExpirationDuration.."];
  145. [self.RCInstances[self.currentNamespace][self.FIRAppName]
  146. fetchWithExpirationDuration:self.expirationLabel.text.integerValue
  147. completionHandler:completion];
  148. }
  149. - (IBAction)fetchAndActivateButtonPressed:(id)sender {
  150. // fetchConfig api callback, this is triggered when client receives response from server
  151. ViewController *__weak weakSelf = self;
  152. FIRRemoteConfigFetchAndActivateCompletion fetchAndActivateCompletion = ^(
  153. FIRRemoteConfigFetchAndActivateStatus status, NSError *error) {
  154. ViewController *strongSelf = weakSelf;
  155. if (!strongSelf) {
  156. return;
  157. }
  158. NSMutableString *output = [@"Fetch and activate status :" mutableCopy];
  159. if (status == FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote) {
  160. [output appendString:@"Success from remote fetch."];
  161. } else if (status == FIRRemoteConfigFetchAndActivateStatusSuccessUsingPreFetchedData) {
  162. [output appendString:@"Success using pre-fetched data."];
  163. } else if (status == FIRRemoteConfigFetchAndActivateStatusError) {
  164. [output
  165. appendString:[NSString stringWithFormat:@"Failure: %@", [error localizedDescription]]];
  166. }
  167. if (error) {
  168. [output appendFormat:@"%@\n", error];
  169. }
  170. if (status == FIRRemoteConfigFetchAndActivateStatusError) {
  171. [output appendString:[NSString stringWithFormat:@"Fetch And Activate Error :%@.\n",
  172. [strongSelf errorString:error.code]]];
  173. if (error.code == FIRRemoteConfigErrorThrottled) {
  174. [output appendString:[NSString stringWithFormat:@"Throttled.\n"]];
  175. }
  176. }
  177. // activate status
  178. [[FRCLog sharedInstance] logToConsole:output];
  179. if (status == FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote ||
  180. status == FIRRemoteConfigFetchAndActivateStatusSuccessUsingPreFetchedData) {
  181. [strongSelf printResult:[[NSMutableString alloc] init]];
  182. }
  183. };
  184. // fetchConfig api call
  185. [self.RCInstances[self.currentNamespace][self.FIRAppName]
  186. fetchAndActivateWithCompletionHandler:fetchAndActivateCompletion];
  187. }
  188. - (IBAction)activateButtonPressed:(id)sender {
  189. [self apply];
  190. }
  191. - (IBAction)refreshButtonPressed:(id)sender {
  192. NSMutableString *output = [[NSMutableString alloc] init];
  193. [self printResult:output];
  194. }
  195. - (IBAction)setDefaultFromPlistButtonPressed:(id)sender {
  196. [self.RCInstances[self.currentNamespace][self.FIRAppName]
  197. setDefaultsFromPlistFileName:@"Defaults"];
  198. [self printDefaultConfigs];
  199. }
  200. - (IBAction)setDefaultButtonPressed:(id)sender {
  201. if (self.configDefaults.count) {
  202. [self.RCInstances[self.currentNamespace][self.FIRAppName] setDefaults:self.configDefaults];
  203. [self.configDefaults removeAllObjects];
  204. [self printDefaultConfigs];
  205. } else {
  206. [[FRCLog sharedInstance] logToConsole:@"Nothing to set for defaults."];
  207. }
  208. }
  209. - (IBAction)onClearLogsButtonPressed:(id)sender {
  210. self.mainTextView.text = @"";
  211. }
  212. - (void)printDefaultConfigs {
  213. NSMutableString *output = [[NSMutableString alloc] init];
  214. [output appendString:@"\n-------Default config------\n"];
  215. NSArray<NSString *> *result = [self.RCInstances[self.currentNamespace][self.FIRAppName]
  216. allKeysFromSource:FIRRemoteConfigSourceDefault];
  217. if (result) {
  218. NSString *stringPerNs = @"";
  219. for (NSString *key in result) {
  220. FIRRemoteConfigValue *value =
  221. [self.RCInstances[self.currentNamespace][self.FIRAppName] defaultValueForKey:key];
  222. stringPerNs = [NSString stringWithFormat:@"%@%@ : %@ : %@\n", stringPerNs,
  223. self.currentNamespace, key, value.stringValue];
  224. }
  225. [output appendString:stringPerNs];
  226. }
  227. [[FRCLog sharedInstance] logToConsole:output];
  228. }
  229. - (IBAction)getValueButtonPressed:(id)sender {
  230. [[FRCLog sharedInstance] logToConsole:[self.RCInstances[self.currentNamespace][self.FIRAppName]
  231. configValueForKey:self.keyLabel.text]
  232. .debugDescription];
  233. }
  234. - (IBAction)logValueButtonPressed:(id)sender {
  235. [[FRCLog sharedInstance]
  236. logToConsole:[NSString stringWithFormat:@"key: %@ logged", self.keyLabel.text]];
  237. }
  238. // add default variable button pressed
  239. - (IBAction)addButtonPressed:(id)sender {
  240. [self addNewEntryToVariables:self.configDefaults isDefaults:YES];
  241. }
  242. - (IBAction)developerModeSwitched:(id)sender {
  243. FIRRemoteConfigSettings *configSettings = [[FIRRemoteConfigSettings alloc] init];
  244. ((FIRRemoteConfig *)(self.RCInstances[self.currentNamespace][self.FIRAppName])).configSettings =
  245. configSettings;
  246. }
  247. - (void)addNewEntryToVariables:(NSMutableDictionary *)variables isDefaults:(BOOL)isDefaults {
  248. if ([self.keyLabel.text length]) {
  249. variables[self.keyLabel.text] = self.valueLabel.text;
  250. NSString *showText = @"custom variables ";
  251. if (isDefaults) {
  252. showText = @"config defaults";
  253. }
  254. [[FRCLog sharedInstance]
  255. logToConsole:[NSString stringWithFormat:@"New %@ added %@ : %@\n", showText,
  256. self.keyLabel.text, self.valueLabel.text]];
  257. self.keyLabel.text = @"";
  258. self.valueLabel.text = @"";
  259. }
  260. }
  261. - (void)apply {
  262. [self.RCInstances[self.currentNamespace][self.FIRAppName]
  263. activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  264. NSMutableString *output = [[NSMutableString alloc] init];
  265. [output appendString:[NSString stringWithFormat:@"ActivateFetched = %@\n",
  266. changed ? @"YES" : @"NO"]];
  267. [[FRCLog sharedInstance] logToConsole:output];
  268. if (!error) {
  269. [self printResult:output];
  270. } else {
  271. [self printResult:[[NSString stringWithFormat:@"Activate failed. Error: %@",
  272. error.localizedDescription] mutableCopy]];
  273. }
  274. }];
  275. }
  276. // print out fetch result
  277. - (void)printResult:(NSMutableString *)output {
  278. FIRRemoteConfig *currentRCInstance = self.RCInstances[self.currentNamespace][self.FIRAppName];
  279. NSString *namespace_p = self.currentNamespace;
  280. [output appendString:@"-------Active config------\n"];
  281. NSArray<NSString *> *result = [self.RCInstances[self.currentNamespace][self.FIRAppName]
  282. allKeysFromSource:FIRRemoteConfigSourceRemote];
  283. if (result) {
  284. NSString *stringPerNs = @"";
  285. for (NSString *key in result) {
  286. FIRRemoteConfigValue *value = [currentRCInstance configValueForKey:key];
  287. stringPerNs = [NSString
  288. stringWithFormat:@"%@%@ : %@ : %@\n", stringPerNs, namespace_p, key, value.stringValue];
  289. }
  290. [output appendString:stringPerNs];
  291. }
  292. [output appendString:@"\n-------Default config------\n"];
  293. result = [currentRCInstance allKeysFromSource:FIRRemoteConfigSourceDefault];
  294. if (result) {
  295. NSString *stringPerNs = @"";
  296. for (NSString *key in result) {
  297. FIRRemoteConfigValue *value = [currentRCInstance defaultValueForKey:key];
  298. stringPerNs = [NSString
  299. stringWithFormat:@"%@%@ : %@ : %@\n", stringPerNs, namespace_p, key, value.stringValue];
  300. }
  301. [output appendString:stringPerNs];
  302. }
  303. [output appendString:@"\n--------Custom Variables--------\n"];
  304. [output appendString:@"\n----------Last fetch time----------------\n"];
  305. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  306. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  307. [output
  308. appendString:[NSString stringWithFormat:@"%@\n",
  309. [dateFormatter
  310. stringFromDate:currentRCInstance.lastFetchTime]]];
  311. [output appendString:@"\n-----------Last fetch status------------\n"];
  312. [output appendString:[NSString
  313. stringWithFormat:@"%@\n",
  314. [self statusString:currentRCInstance.lastFetchStatus]]];
  315. FIRInstallations *installations = [FIRInstallations installations];
  316. [installations installationIDWithCompletion:^(NSString *_Nullable identifier,
  317. NSError *_Nullable error) {
  318. [output appendString:@"\n-----------Installation ID------------------\n"];
  319. [output appendString:[NSString stringWithFormat:@"%@\n", identifier]];
  320. [output appendString:@"\n-----------Installation ID token------------\n"];
  321. [installations authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  322. NSError *_Nullable error) {
  323. [output appendString:[NSString stringWithFormat:@"%@\n", tokenResult.authToken]];
  324. [[FRCLog sharedInstance] logToConsole:output];
  325. }];
  326. }];
  327. }
  328. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  329. [textField resignFirstResponder];
  330. return YES;
  331. }
  332. - (NSString *)statusString:(FIRRemoteConfigFetchStatus)status {
  333. switch (status) {
  334. case FIRRemoteConfigFetchStatusSuccess:
  335. return @"Success";
  336. case FIRRemoteConfigFetchStatusNoFetchYet:
  337. return @"NotFetchYet";
  338. case FIRRemoteConfigFetchStatusFailure:
  339. return @"Failure";
  340. case FIRRemoteConfigFetchStatusThrottled:
  341. return @"Throttled";
  342. default:
  343. return @"Unknown";
  344. }
  345. return @"";
  346. }
  347. - (NSString *)errorString:(FIRRemoteConfigError)error {
  348. switch (error) {
  349. case FIRRemoteConfigErrorInternalError:
  350. return @"Internal Error";
  351. case FIRRemoteConfigErrorUnknown:
  352. return @"Unknown Error";
  353. case FIRRemoteConfigErrorThrottled:
  354. return @"Throttled";
  355. default:
  356. return @"unknown";
  357. }
  358. return @"";
  359. }
  360. - (IBAction)fetchIIDButtonClicked:(id)sender {
  361. FIRInstallations *installations =
  362. [FIRInstallations installationsWithApp:[FIRApp appNamed:self.FIRAppName]];
  363. [installations installationIDWithCompletion:^(NSString *_Nullable identifier,
  364. NSError *_Nullable error) {
  365. if (error) {
  366. [[FRCLog sharedInstance] logToConsole:[NSString stringWithFormat:@"%@", error]];
  367. } else {
  368. [installations authTokenWithCompletion:^(
  369. FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  370. NSError *_Nullable error) {
  371. if (tokenResult.authToken) {
  372. ((FIRRemoteConfig *)self.RCInstances[self.currentNamespace][self.FIRAppName])
  373. .settings.configInstallationsToken = tokenResult.authToken;
  374. [[FRCLog sharedInstance]
  375. logToConsole:[NSString
  376. stringWithFormat:
  377. @"Successfully got installation ID : \n\n%@\n\nToken : \n\n%@\n",
  378. identifier, tokenResult.authToken]];
  379. }
  380. }];
  381. }
  382. }];
  383. }
  384. - (IBAction)searchButtonClicked:(id)sender {
  385. NSString *output = @"-------Active Config------\n";
  386. for (NSString *key in [self.RCInstances[self.currentNamespace][self.FIRAppName]
  387. keysWithPrefix:self.keyLabel.text]) {
  388. FIRRemoteConfigValue *value =
  389. ((FIRRemoteConfig *)(self.RCInstances[self.currentNamespace][self.FIRAppName]))[key];
  390. output = [NSString stringWithFormat:@"%@%@ : %@ : %@\n", output, self.currentNamespace, key,
  391. value.stringValue];
  392. }
  393. [[FRCLog sharedInstance] logToConsole:output];
  394. }
  395. - (IBAction)userPropertyButtonClicked:(id)sender {
  396. [FIRAnalytics setUserPropertyString:self.valueLabel.text forName:self.keyLabel.text];
  397. NSString *output = [NSString
  398. stringWithFormat:@"Set User Property => %@ : %@\n", self.keyLabel.text, self.valueLabel.text];
  399. [[FRCLog sharedInstance] logToConsole:output];
  400. }
  401. #pragma mark - picker
  402. // The number of columns of data
  403. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  404. // App and currentNamespace pickers.
  405. return 2;
  406. }
  407. // The number of rows of data
  408. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  409. NSInteger rowCount = (component == 0) ? self.namespacePickerData.count : self.appPickerData.count;
  410. return rowCount;
  411. }
  412. // The data to return for the row and component (column) that's being passed in
  413. - (NSString *)pickerView:(UIPickerView *)pickerView
  414. titleForRow:(NSInteger)row
  415. forComponent:(NSInteger)component {
  416. if (component == 0) {
  417. self.currentNamespace = self.namespacePickerData[row];
  418. return self.namespacePickerData[row];
  419. } else {
  420. self.FIRAppName = self.appPickerData[row];
  421. return self.appPickerData[row];
  422. }
  423. }
  424. - (UIView *)pickerView:(UIPickerView *)pickerView
  425. viewForRow:(NSInteger)row
  426. forComponent:(NSInteger)component
  427. reusingView:(UIView *)view {
  428. UILabel *tView = (UILabel *)view;
  429. if (!tView) {
  430. tView = [[UILabel alloc] init];
  431. [tView setFont:[UIFont fontWithName:@"Helvetica" size:15]];
  432. tView.numberOfLines = 3;
  433. }
  434. if (component == 0) {
  435. self.currentNamespace = self.namespacePickerData[row];
  436. tView.text = self.namespacePickerData[row];
  437. } else {
  438. self.FIRAppName = self.appPickerData[row];
  439. tView.text = self.appPickerData[row];
  440. }
  441. return tView;
  442. }
  443. @end