BuildDetails.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // LoopFollow
  2. // BuildDetails.swift
  3. // Created by Jonas Björkert on 2024-03-25.
  4. import Foundation
  5. class BuildDetails {
  6. static var `default` = BuildDetails()
  7. let dict: [String: Any]
  8. init() {
  9. guard let url = Bundle.main.url(forResource: "BuildDetails", withExtension: "plist"),
  10. let data = try? Data(contentsOf: url),
  11. let parsed = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]
  12. else {
  13. dict = [:]
  14. return
  15. }
  16. dict = parsed
  17. }
  18. var buildDateString: String? {
  19. return dict["com-LoopFollow-build-date"] as? String
  20. }
  21. var branch: String? {
  22. return dict["com-LoopFollow-branch"] as? String
  23. }
  24. var branchAndSha: String {
  25. let branch = branch ?? "Unknown"
  26. let sha = dict["com-LoopFollow-commit-sha"] as? String ?? "Unknown"
  27. return "\(branch) \(sha)"
  28. }
  29. // Determine if the build is from TestFlight
  30. func isTestFlightBuild() -> Bool {
  31. #if targetEnvironment(simulator)
  32. return false
  33. #else
  34. if Bundle.main.url(forResource: "embedded", withExtension: "mobileprovision") != nil {
  35. return false
  36. }
  37. guard let receiptName = Bundle.main.appStoreReceiptURL?.lastPathComponent else {
  38. return false
  39. }
  40. return "sandboxReceipt".caseInsensitiveCompare(receiptName) == .orderedSame
  41. #endif
  42. }
  43. // Determine if the build is for Simulator
  44. func isSimulatorBuild() -> Bool {
  45. #if targetEnvironment(simulator)
  46. return true
  47. #else
  48. return false
  49. #endif
  50. }
  51. // Determine if the build is for Mac
  52. func isMacApp() -> Bool {
  53. #if targetEnvironment(macCatalyst)
  54. return true
  55. #else
  56. return false
  57. #endif
  58. }
  59. // Parse the build date string into a Date object
  60. func buildDate() -> Date? {
  61. guard let dateString = dict["com-LoopFollow-build-date"] as? String else {
  62. return nil
  63. }
  64. let formatter = ISO8601DateFormatter()
  65. return formatter.date(from: dateString)
  66. }
  67. // Calculate the expiration date based on the build type
  68. func calculateExpirationDate() -> Date {
  69. if isTestFlightBuild(), let buildDate = buildDate() {
  70. // For TestFlight, add 90 days to the build date
  71. return Calendar.current.date(byAdding: .day, value: 90, to: buildDate)!
  72. } else {
  73. // For Xcode builds, use the provisioning profile's expiration date
  74. if let provision = MobileProvision.read() {
  75. return provision.expirationDate
  76. } else {
  77. return .distantFuture
  78. }
  79. }
  80. }
  81. // Expiration header based on build type
  82. var expirationHeaderString: String {
  83. if isTestFlightBuild() {
  84. return "TestFlight Expires"
  85. } else {
  86. return "App Expires"
  87. }
  88. }
  89. }