|
| 1 | +// |
| 2 | +// AppDelegate.swift |
| 3 | +// FlagsmithClient |
| 4 | +// |
| 5 | +// Created by Tomash Tsiupiak on 06/20/2019. |
| 6 | +// Copyright (c) 2019 Tomash Tsiupiak. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import FlagsmithClient |
| 11 | +#if canImport(SwiftUI) |
| 12 | + import SwiftUI |
| 13 | +#endif |
| 14 | + |
| 15 | +func isSuccess<T, F>(_ result: Result<T, F>) -> Bool { |
| 16 | + if case .success = result { return true } else { return false } |
| 17 | +} |
| 18 | + |
| 19 | +@UIApplicationMain |
| 20 | +class AppDelegate: UIResponder, UIApplicationDelegate { |
| 21 | + |
| 22 | + var window: UIWindow? |
| 23 | + let concurrentQueue = DispatchQueue(label: "concurrentQueue", qos: .default, attributes: .concurrent) |
| 24 | + |
| 25 | + func application(_ application: UIApplication, |
| 26 | + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 27 | + // Override point for customization after application launch. |
| 28 | + Flagsmith.shared.apiKey = "<add your API key from the Flagsmith dashboard here>" |
| 29 | + |
| 30 | + // set default flags |
| 31 | + Flagsmith.shared.defaultFlags = [Flag(featureName: "feature_a", enabled: false), |
| 32 | + Flag(featureName: "font_size", intValue: 12, enabled: true), |
| 33 | + Flag(featureName: "my_name", stringValue: "Testing", enabled: true)] |
| 34 | + |
| 35 | + // set cache on / off (defaults to off) |
| 36 | + Flagsmith.shared.cacheConfig.useCache = true |
| 37 | + |
| 38 | + // set custom cache to use (defaults to shared URLCache) |
| 39 | + // Flagsmith.shared.cacheConfig.cache = <CUSTOM_CACHE> |
| 40 | + |
| 41 | + // set skip API on / off (defaults to off) |
| 42 | + Flagsmith.shared.cacheConfig.skipAPI = false |
| 43 | + |
| 44 | + // set cache TTL in seconds (defaults to 0, i.e. infinite) |
| 45 | + Flagsmith.shared.cacheConfig.cacheTTL = 90 |
| 46 | + |
| 47 | + // set analytics on or off |
| 48 | + Flagsmith.shared.enableAnalytics = true |
| 49 | + |
| 50 | + // Enable real time updates |
| 51 | + Flagsmith.shared.enableRealtimeUpdates = true |
| 52 | + |
| 53 | + // set the analytics flush period in seconds |
| 54 | + Flagsmith.shared.analyticsFlushPeriod = 10 |
| 55 | + |
| 56 | + Flagsmith.shared.getFeatureFlags { (result) in |
| 57 | + print(result) |
| 58 | + } |
| 59 | + Flagsmith.shared.hasFeatureFlag(withID: "freeze_delinquent_accounts") { (result) in |
| 60 | + print(result) |
| 61 | + } |
| 62 | + |
| 63 | + // Try getting the feature flags concurrently to ensure that this does not cause any issues |
| 64 | + // This was originally highlighted in https://github.com/Flagsmith/flagsmith-ios-client/pull/40 |
| 65 | + for _ in 1...20 { |
| 66 | + concurrentQueue.async { |
| 67 | + Flagsmith.shared.getFeatureFlags { (_) in |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Flagsmith.shared.setTrait(Trait(key: "<my_key>", value: "<my_value>"), forIdentity: "<my_identity>") { (result) in print(result) } |
| 73 | + // Flagsmith.shared.getIdentity("<my_key>") { (result) in print(result) } |
| 74 | + |
| 75 | + // Launch SwiftUIView |
| 76 | + if #available(iOS 13.0, *) { |
| 77 | + let swiftUIView = SwiftUIView() |
| 78 | + window = UIWindow(frame: UIScreen.main.bounds) |
| 79 | + window?.rootViewController = UIHostingController(rootView: swiftUIView) |
| 80 | + window?.makeKeyAndVisible() |
| 81 | + } |
| 82 | + |
| 83 | + return true |
| 84 | + } |
| 85 | + |
| 86 | + func applicationWillResignActive(_ application: UIApplication) { |
| 87 | + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. |
| 88 | + // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. |
| 89 | + } |
| 90 | + |
| 91 | + func applicationDidEnterBackground(_ application: UIApplication) { |
| 92 | + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. |
| 93 | + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. |
| 94 | + } |
| 95 | + |
| 96 | + func applicationWillEnterForeground(_ application: UIApplication) { |
| 97 | + // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. |
| 98 | + } |
| 99 | + |
| 100 | + func applicationDidBecomeActive(_ application: UIApplication) { |
| 101 | + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. |
| 102 | + } |
| 103 | + |
| 104 | + func applicationWillTerminate(_ application: UIApplication) { |
| 105 | + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. |
| 106 | + } |
| 107 | + |
| 108 | + #if swift(>=5.5.2) |
| 109 | + /// (Example) Setup the app based on the available feature flags. |
| 110 | + /// |
| 111 | + /// **Flagsmith** supports the Swift Concurrency feature `async`/`await`. |
| 112 | + /// Requests and logic can be handled in a streamlined order, |
| 113 | + /// eliminating the need to nest multiple completion handlers. |
| 114 | + @available(iOS 13.0, *) |
| 115 | + func determineAppConfiguration() async { |
| 116 | + let flagsmith = Flagsmith.shared |
| 117 | + |
| 118 | + do { |
| 119 | + if try await flagsmith.hasFeatureFlag(withID: "ab_test_enabled") { |
| 120 | + if let theme = try await flagsmith.getValueForFeature(withID: "app_theme") { |
| 121 | + setTheme(theme) |
| 122 | + } else { |
| 123 | + let flags = try await flagsmith.getFeatureFlags() |
| 124 | + processFlags(flags) |
| 125 | + } |
| 126 | + } else { |
| 127 | + let trait = Trait(key: "selected_tint_color", value: "orange") |
| 128 | + let identity = "4DDBFBCA-3B6E-4C59-B107-954F84FD7F6D" |
| 129 | + try await flagsmith.setTrait(trait, forIdentity: identity) |
| 130 | + } |
| 131 | + } catch { |
| 132 | + print(error) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + func setTheme(_ theme: TypedValue) {} |
| 137 | + func processFlags(_ flags: [Flag]) {} |
| 138 | + #endif |
| 139 | +} |
0 commit comments