BuildDetails.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // BuildDetails.swift
  3. // Trio
  4. //
  5. // Created by Jonas Björkert on 2024-05-09.
  6. //
  7. import Foundation
  8. class BuildDetails {
  9. static var `default` = BuildDetails()
  10. let dict: [String: Any]
  11. init() {
  12. guard let url = Bundle.main.url(forResource: "BuildDetails", withExtension: "plist"),
  13. let data = try? Data(contentsOf: url),
  14. let parsed = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]
  15. else {
  16. dict = [:]
  17. return
  18. }
  19. dict = parsed
  20. }
  21. var buildDateString: String? {
  22. dict["com-trio-build-date"] as? String
  23. }
  24. var branchAndSha: String {
  25. let branch = dict["com-trio-branch"] as? String ?? "Unknown"
  26. let sha = dict["com-trio-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. // Parse the build date string into a Date object
  44. func buildDate() -> Date? {
  45. let dateFormatter = DateFormatter()
  46. dateFormatter.dateFormat = "EEE MMM d HH:mm:ss 'UTC' yyyy"
  47. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  48. dateFormatter.timeZone = TimeZone(identifier: "UTC")
  49. guard let dateString = buildDateString,
  50. let date = dateFormatter.date(from: dateString)
  51. else {
  52. return nil
  53. }
  54. return date
  55. }
  56. // Calculate the expiration date based on the build type
  57. func calculateExpirationDate() -> Date? {
  58. if isTestFlightBuild(), let buildDate = buildDate() {
  59. // For TestFlight, add 90 days to the build date
  60. return Calendar.current.date(byAdding: .day, value: 90, to: buildDate)!
  61. } else {
  62. return Bundle.main.profileExpirationDate
  63. }
  64. }
  65. // Expiration header based on build type
  66. var expirationHeaderString: String {
  67. if isTestFlightBuild() {
  68. return "Beta (TestFlight) Expires"
  69. } else {
  70. return "App Expires"
  71. }
  72. }
  73. }