BuildDetails.swift 2.9 KB

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