### Setup AppVersionMonitor in AppDelegate (Swift) Source: https://github.com/eure/appversionmonitor/blob/master/README.md Initializes the AppVersionMonitor when the application finishes launching. This is a crucial setup step for the library to start monitoring app versions. ```swift func applicationDidFinishLaunching(application: UIApplication) { AppVersionMonitor.sharedMonitor.startup() } ``` -------------------------------- ### Get Installed Versions (Swift) Source: https://github.com/eure/appversionmonitor/blob/master/README.md Fetches a list of all previously installed versions of the application. This is useful for understanding the installation history. ```swift let installedVersions: [AppVersion] = AppVersionMonitor.sharedMonitor.installedVersions ``` -------------------------------- ### Track Version History Swift Source: https://context7.com/eure/appversionmonitor/llms.txt This example shows how to access and analyze the complete installation history of an app using AppVersionMonitor. It demonstrates iterating through installed versions, checking version counts, finding the first installed version, and detecting skipped major version updates. ```swift import AppVersionMonitor func analyzeVersionHistory() { let monitor = AppVersionMonitor.sharedMonitor let history = monitor.installedVersions print("Total versions installed: \(history.count)") // Print all versions for (index, version) in history.enumerated() { print("\(index + 1). Version \(version.versionString)") } // Output: // 1. Version 1.0.0 // 2. Version 1.1.0 // 3. Version 1.2.0 // 4. Version 1.4.2 // Check if user is a beta tester if history.count > 5 { print("Power user detected - \(history.count) versions installed") offerBetaTesterRole() } // Find first installed version if let firstVersion = history.first { print("First installed version: \(firstVersion.versionString)") if firstVersion < "1.0.0" { print("Early adopter!") } } // Check if user skipped versions if let current = history.last, let previous = history.dropLast().last { let currentMajor = getMajorVersion(current) let previousMajor = getMajorVersion(previous) if currentMajor > previousMajor + 1 { print("User skipped major version(s)") showComprehensiveChangelog() } } } func getMajorVersion(_ version: AppVersion) -> Int { let components = version.versionString.split(separator: ".") return Int(components.first ?? "0") ?? 0 } func offerBetaTesterRole() { // Invite user to beta program } func showComprehensiveChangelog() { // Show detailed changelog for skipped versions } ``` -------------------------------- ### Get Marketing Version (Swift) Source: https://github.com/eure/appversionmonitor/blob/master/README.md Retrieves the current marketing version of the application, represented by CFBundleShortVersionString. It provides both the AppVersion object and its string representation. ```swift let currentVersion: AppVersion = AppVersion.marketingVersion let versionString: String = AppVersion.marketingVersion.versionString // "1.2.3" ``` -------------------------------- ### Check App Version Status (Swift) Source: https://github.com/eure/appversionmonitor/blob/master/README.md Uses a switch statement to determine the current state of the app version, such as if it's newly installed, unchanged, upgraded, or downgraded. This enables conditional logic based on version changes. ```swift switch AppVersionMonitor.sharedMonitor.state { case .Installed: // Do something when app installed. // Happy! 🍻 // ex. Start tutorial. case .NotChanged: // Do something when version not changed. // Peace 😌 // Nothing to do? case .Upgraded(let previousVersion: AppVersion): // Do something when version upgraded. // Yeah! 😝 // ex. Migrate App Data. case .Downgraded(let previousVersion: AppVersion): // Do something when version downgraded. (Impossible normally) // What happened? 😵 // ex. Purge App Data. } ``` -------------------------------- ### Handle App Version State Changes Source: https://context7.com/eure/appversionmonitor/llms.txt Illustrates how to handle different application version states (installed, notChanged, upgraded, downgraded) using a switch statement on the monitor's state property. This allows for conditional logic based on version changes. ```swift import AppVersionMonitor func handleVersionState() { switch AppVersionMonitor.sharedMonitor.state { case .installed: // First time installation - show onboarding print("Welcome! This is your first launch.") showTutorial() trackAnalyticsEvent(name: "first_install") case .notChanged: // Same version as previous launch print("App version unchanged") // No migration needed case .upgraded(let previousVersion): // App was upgraded from previous version print("Upgraded from version (previousVersion.versionString)") // Perform data migration based on previous version if previousVersion < "2.0.0" { migrateDatabaseSchema() print("Migrated database from v1.x to v2.x") } // Show what's new screen showWhatsNew(from: previousVersion) case .downgraded(let previousVersion): // App was downgraded (rare but possible in development) print("Downgraded from version (previousVersion.versionString)") // Purge incompatible cached data clearCache() print("Cache cleared due to version downgrade") } } // Example helper functions func showTutorial() { // Present tutorial view controller } func migrateDatabaseSchema() { // Perform database migration } func showWhatsNew(from version: AppVersion) { // Display release notes } func clearCache() { // Remove cached data } func trackAnalyticsEvent(name: String) { // Send analytics event } ``` -------------------------------- ### Track App Version Changes with Analytics (Swift) Source: https://context7.com/eure/appversionmonitor/llms.txt Integrates AppVersionMonitor with an analytics service to track events such as app installation, upgrades, downgrades, and general app opens. It also demonstrates setting user properties based on the app version. Dependencies include 'AppVersionMonitor' and a mock 'Analytics' class. ```swift import AppVersionMonitor func trackVersionAnalytics() { let monitor = AppVersionMonitor.sharedMonitor let currentVersion = AppVersion.marketingVersion // Track version state as analytics event switch monitor.state { case .installed: // Track new installation Analytics.track(event: "app_installed", properties: [ "version": currentVersion.versionString, "platform": "iOS" ]) case .upgraded(let previousVersion): // Track upgrade event Analytics.track(event: "app_upgraded", properties: [ "from_version": previousVersion.versionString, "to_version": currentVersion.versionString, "versions_skipped": calculateSkippedVersions(from: previousVersion, to: currentVersion) ]) // Set user properties Analytics.setUserProperty(key: "app_version", value: currentVersion.versionString) Analytics.setUserProperty(key: "total_versions_installed", value: monitor.installedVersions.count) case .downgraded(let previousVersion): // Track downgrade (unusual - worth investigating) Analytics.track(event: "app_downgraded", properties: [ "from_version": previousVersion.versionString, "to_version": currentVersion.versionString ]) case .notChanged: // Track app open with version info Analytics.track(event: "app_opened", properties: [ "version": currentVersion.versionString ]) } } func calculateSkippedVersions(from: AppVersion, to: AppVersion) -> Int { // Calculate how many versions were skipped let fromComponents = from.versionString.split(separator: ".").compactMap { Int($0) } let toComponents = to.versionString.split(separator: ".").compactMap { Int($0) } if fromComponents.count > 1 && toComponents.count > 1 { return (toComponents[1] - fromComponents[1]) - 1 } return 0 } // Mock Analytics class for example class Analytics { static func track(event: String, properties: [String: Any]) { print("Analytics event: \(event), properties: \(properties)") } static func setUserProperty(key: String, value: Any) { print("Set user property: \(key) = \(value)") } } ``` -------------------------------- ### Access AppVersionMonitor Singleton and Startup Source: https://context7.com/eure/appversionmonitor/llms.txt Demonstrates how to access the shared AppVersionMonitor singleton instance and call its startup method in the AppDelegate. This is the primary way to interact with the library for version tracking. ```swift import AppVersionMonitor // Access singleton instance let monitor = AppVersionMonitor.sharedMonitor // Call startup in AppDelegate func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { AppVersionMonitor.sharedMonitor.startup() return true } // Check current version state let currentState = monitor.state print("Version state: (currentState)") // Get all installed versions let allVersions = monitor.installedVersions print("Installed versions: (allVersions)") // Output: Installed versions: [1.0.0, 1.2.0, 1.4.2] ``` -------------------------------- ### Compare App Versions (Swift) Source: https://github.com/eure/appversionmonitor/blob/master/README.md Allows for easy comparison between different app versions using standard comparison operators. Supports comparing AppVersion objects with strings or other AppVersion objects. ```swift AppVersion.marketingVersion > AppVersion("1.2.3") AppVersion("1.2.3") < AppVersion("3.2.1") AppVersion("1.2.3") < "3.2.1" ``` -------------------------------- ### Create and Compare AppVersion Objects Source: https://context7.com/eure/appversionmonitor/llms.txt Shows how to create AppVersion objects from strings, access current marketing and build versions, and perform comparisons using standard operators. This is useful for feature gating and version-specific logic. ```swift import AppVersionMonitor // Create AppVersion instances let version1 = AppVersion("1.2.3") let version2 = AppVersion("1.5.0") // String literal syntax let version3: AppVersion = "2.0.0" // Get current app version from Bundle let currentVersion = AppVersion.marketingVersion let buildVersion = AppVersion.version print("Current version: (currentVersion.versionString)") print("Build version: (buildVersion.versionString)") // Version comparison operators if version1 < version2 { print("1.2.3 is less than 1.5.0") // ✓ This prints } if version1 <= version2 { print("1.2.3 is less than or equal to 1.5.0") // ✓ This prints } if version1 == AppVersion("1.2.3") { print("Versions are equal") // ✓ This prints } if version2 > version1 { print("1.5.0 is greater than 1.2.3") // ✓ This prints } if version3 >= version2 { print("2.0.0 is greater than or equal to 1.5.0") // ✓ This prints } // Practical example: Feature gating if AppVersion.marketingVersion >= "2.0.0" { enableNewFeatures() print("New features enabled for v2.0+") } else { print("Running legacy feature set") } func enableNewFeatures() { // Implementation to enable new features } ``` -------------------------------- ### Implement Version-Based Feature Flags (Swift) Source: https://context7.com/eure/appversionmonitor/llms.txt Demonstrates how to create feature flags that are enabled or disabled based on the current marketing version of the app, using AppVersionMonitor. This allows for gradual rollouts or version-specific features. It defines properties for features like dark mode and premium features, checking them against version strings. ```swift import AppVersionMonitor class FeatureFlags { static let shared = FeatureFlags() private let currentVersion = AppVersion.marketingVersion // Feature: Dark mode (available from v2.0.0) var isDarkModeEnabled: Bool { return currentVersion >= "2.0.0" } // Feature: Premium features (v2.5.0+) var isPremiumFeaturesEnabled: Bool { return currentVersion >= "2.5.0" } // Feature: New UI redesign (v3.0.0+) var isNewUIEnabled: Bool { return currentVersion >= "3.0.0" } // Feature: Experimental AI features (v3.2.0+) var isAIFeaturesEnabled: Bool { return currentVersion >= "3.2.0" } } // Usage in app code func configureUI() { let features = FeatureFlags.shared if features.isDarkModeEnabled { print("Dark mode is available") showDarkModeToggle() } if features.isPremiumFeaturesEnabled { print("Premium features unlocked") enablePremiumContent() } if features.isNewUIEnabled { print("Loading new UI") loadModernInterface() } else { print("Loading legacy UI") loadLegacyInterface() } if features.isAIFeaturesEnabled { print("AI features available") initializeAIEngine() } } func showDarkModeToggle() { // Show dark mode option in settings } func enablePremiumContent() { // Unlock premium features } func loadModernInterface() { // Load redesigned UI } func loadLegacyInterface() { // Load old UI } func initializeAIEngine() { // Initialize AI features } ``` -------------------------------- ### Handle Variable-Length Version Numbers Swift Source: https://context7.com/eure/appversionmonitor/llms.txt This snippet demonstrates how AppVersionMonitor automatically handles version strings with varying numbers of components by padding with zeros. It shows equality checks and comparisons between versions of different lengths, ensuring correct numeric comparison. ```swift import AppVersionMonitor // Different length version numbers let v1 = AppVersion("1.2") let v2 = AppVersion("1.2.0") let v3 = AppVersion("1.2.0.0") // All three are equal (trailing zeros added automatically) assert(v1 == v2) // true assert(v2 == v3) // true assert(v1 == v3) // true print("\(v1) == \(v2) == \(v3)") // Output: 1.2 == 1.2.0 == 1.2.0.0 // Comparison with different lengths let short = AppVersion("1.4.5") let long = AppVersion("1.4.5.4") if short < long { print("1.4.5 < 1.4.5.4") // ✓ This prints (1.4.5.0 < 1.4.5.4) } // Very long version strings let veryLong = AppVersion("1.4.5.3.4.6") if short < veryLong { print("1.4.5 < 1.4.5.3.4.6") // ✓ This prints } // Numeric comparison (not lexicographic) let v10 = AppVersion("3.10.1") let v6 = AppVersion("3.6.0") if v10 > v6 { print("3.10.1 > 3.6.0 (numeric comparison)") // ✓ This prints // Note: lexicographic comparison would incorrectly say "3.10" < "3.6" } ``` -------------------------------- ### Conditional Migration Based on Version Swift Source: https://context7.com/eure/appversionmonitor/llms.txt This code demonstrates how to perform targeted data migrations based on the application's previous version. It uses a switch statement on the monitor's state to identify upgrades and then applies specific migration functions based on version comparisons. ```swift import AppVersionMonitor func performVersionMigrations() { switch AppVersionMonitor.sharedMonitor.state { case .upgraded(let previousVersion): print("Migrating from \(previousVersion.versionString)") // Migration 1: Database schema change in v1.5.0 if previousVersion < "1.5.0" { print("Applying migration: Add user preferences table") addUserPreferencesTable() } // Migration 2: Cache format change in v2.0.0 if previousVersion < "2.0.0" { print("Applying migration: Convert cache to new format") migrateCacheFormat() } // Migration 3: API endpoint change in v2.3.0 if previousVersion < "2.3.0" { print("Applying migration: Update API configuration") updateAPIEndpoints() } // Migration 4: Data encryption in v3.0.0 if previousVersion < "3.0.0" { print("Applying migration: Encrypt sensitive data") encryptUserData() } print("All migrations completed successfully") case .installed: print("Fresh install - no migrations needed") initializeDefaultData() case .notChanged: print("No version change - skipping migrations") case .downgraded(let previousVersion): print("Downgraded from \(previousVersion.versionString)") // Handle downgrade scenario resetToSafeState() } } // Example migration functions func addUserPreferencesTable() { // Execute SQL: CREATE TABLE user_preferences... } func migrateCacheFormat() { // Convert old cache format to new format } func updateAPIEndpoints() { // Update stored API configuration } func encryptUserData() { // Encrypt sensitive user data } func initializeDefaultData() { // Set up initial app data } func resetToSafeState() { // Clear incompatible data } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.