ViewController.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2020 Google LLC
  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 FirebaseAnalytics
  17. import FirebaseRemoteConfig
  18. import UIKit
  19. class ViewController: UIViewController {
  20. @IBAction func fireAnalyticsEventButtonTapped(_ sender: Any) {
  21. let alert = UIAlertController(title: "Fire custom analytics event",
  22. message: "Enter the name of a custom event",
  23. preferredStyle: .alert)
  24. alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: { action in
  25. alert.dismiss(animated: true, completion: nil)
  26. }))
  27. // Fire Analytics event based on text field content.
  28. alert.addAction(UIAlertAction(title: "Fire event", style: .default, handler: { action in
  29. guard let textField = alert.textFields?.first else {
  30. assertionFailure("Alert has no text field. Something went wrong.")
  31. return
  32. }
  33. if let event = textField.text {
  34. Analytics.logEvent(event, parameters: nil)
  35. }
  36. }))
  37. alert.addTextField { textField in
  38. textField.placeholder = "e.g.: tapped_close_button"
  39. }
  40. present(alert, animated: true, completion: nil)
  41. }
  42. @IBAction func checkTestButtonTapped(_ sender: Any) {
  43. let remoteConfig = RemoteConfig.remoteConfig()
  44. remoteConfig.fetchAndActivate { status, error in
  45. // Fetch `bg_color` from remote config after activating.
  46. guard let color = remoteConfig.configValue(forKey: "bg_color").stringValue else {
  47. assertionFailure("Failed to fetch `bg_color` config value. Check the ABT console.")
  48. return
  49. }
  50. // Set background color.
  51. DispatchQueue.main.async {
  52. // The AB test has two values for `bg_color`: "green or "red".
  53. if color == "green" {
  54. self.view.backgroundColor = UIColor.green
  55. } else {
  56. self.view.backgroundColor = UIColor.red
  57. }
  58. }
  59. }
  60. }
  61. }