Bundle+Extensions.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 profileExpirationDateString: String? {
  10. guard
  11. let profilePath = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision"),
  12. let profileData = try? Data(contentsOf: URL(fileURLWithPath: profilePath)),
  13. // Note: We use `NSString` instead of `String`, because it makes it easier working with regex, ranges, substring etc.
  14. let profileNSString = NSString(data: profileData, encoding: String.Encoding.ascii.rawValue)
  15. else {
  16. print(
  17. "WARNING: Could not find or read `embedded.mobileprovision`. If running on Simulator, there are no provisioning profiles."
  18. )
  19. return nil
  20. }
  21. let regexPattern = "<key>ExpirationDate</key>[\\W]*?<date>(.*?)</date>"
  22. guard let regex = try? NSRegularExpression(pattern: regexPattern, options: []),
  23. let match = regex.firstMatch(
  24. in: profileNSString as String,
  25. options: [],
  26. range: NSRange(location: 0, length: profileNSString.length)
  27. ),
  28. let range = Range(match.range(at: 1), in: profileNSString as String)
  29. else {
  30. print("Warning: Could not create regex or find match.")
  31. return nil
  32. }
  33. return String(profileNSString.substring(with: NSRange(range, in: profileNSString as String)))
  34. }
  35. var profileExpirationDate: Date? {
  36. guard let dateString = profileExpirationDateString else { return nil }
  37. let dateFormatter = DateFormatter()
  38. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
  39. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  40. dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
  41. return dateFormatter.date(from: dateString)
  42. }
  43. }