Bundle+Extensions.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import Foundation
  2. extension Bundle {
  3. var releaseVersionNumber: String? {
  4. infoDictionary?["CFBundleShortVersionString"] as? String
  5. }
  6. var buildVersionNumber: String? {
  7. infoDictionary?["CFBundleVersion"] as? String
  8. }
  9. var buildDate: Date {
  10. if let infoPath = Bundle.main.path(forResource: "Info", ofType: "plist"),
  11. let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath),
  12. let infoDate = infoAttr[.modificationDate] as? Date
  13. {
  14. return infoDate
  15. }
  16. return Date()
  17. }
  18. var profileExpiration: String {
  19. guard
  20. let profilePath = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision"),
  21. let profileData = try? Data(contentsOf: URL(fileURLWithPath: profilePath)),
  22. // Note: We use `NSString` instead of `String`, because it makes it easier working with regex, ranges, substring etc.
  23. let profileNSString = NSString(data: profileData, encoding: String.Encoding.ascii.rawValue)
  24. else {
  25. print(
  26. "WARNING: Could not find or read `embedded.mobileprovision`. If running on Simulator, there are no provisioning profiles."
  27. )
  28. return "N/A"
  29. }
  30. // NOTE: We have the `[\\W]*?` check to make sure that variations in number of tabs or new lines in the future does not influence the result.
  31. guard let regex = try? NSRegularExpression(pattern: "<key>ExpirationDate</key>[\\W]*?<date>(.*?)</date>", options: [])
  32. else {
  33. print("Warning: Could not create regex.")
  34. return "N/A"
  35. }
  36. let regExMatches = regex.matches(
  37. in: profileNSString as String,
  38. options: [],
  39. range: NSRange(location: 0, length: profileNSString.length)
  40. )
  41. // NOTE: range `0` corresponds to the full regex match, so to get the first capture group, we use range `1`
  42. guard let rangeOfCapturedGroupForDate = regExMatches.first?.range(at: 1) else {
  43. print("Warning: Could not find regex match or capture group.")
  44. return "N/A"
  45. }
  46. let dateWithTimeAsString = profileNSString.substring(with: rangeOfCapturedGroupForDate)
  47. let dateAsStringIndex = dateWithTimeAsString.firstIndex(of: "T")!
  48. let dateAsString = dateWithTimeAsString.substring(to: dateAsStringIndex)
  49. return dateAsString
  50. }
  51. }