ViewController.m 22 KB

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