Verifies min/max OS version before downloading app + asks user to download older app version if necessary

This commit is contained in:
Riley Testut
2022-11-15 17:43:38 -06:00
parent 4fc4d6ba39
commit 5b20ce13bc
2 changed files with 126 additions and 39 deletions

View File

@@ -20,7 +20,6 @@ class DownloadAppOperation: ResultOperation<ALTApplication>
private let appName: String private let appName: String
private let bundleIdentifier: String private let bundleIdentifier: String
private var sourceURL: URL?
private let destinationURL: URL private let destinationURL: URL
private let session = URLSession(configuration: .default) private let session = URLSession(configuration: .default)
@@ -33,7 +32,6 @@ class DownloadAppOperation: ResultOperation<ALTApplication>
self.appName = app.name self.appName = app.name
self.bundleIdentifier = app.bundleIdentifier self.bundleIdentifier = app.bundleIdentifier
self.sourceURL = app.url
self.destinationURL = destinationURL self.destinationURL = destinationURL
super.init() super.init()
@@ -54,9 +52,88 @@ class DownloadAppOperation: ResultOperation<ALTApplication>
print("Downloading App:", self.bundleIdentifier) print("Downloading App:", self.bundleIdentifier)
guard let sourceURL = self.sourceURL else { return self.finish(.failure(OperationError.appNotFound(name: self.appName))) } // Set _after_ checking self.context.error to prevent overwriting localized failure for previous errors.
self.localizedFailure = String(format: NSLocalizedString("%@ could not be downloaded.", comment: ""), self.appName)
self.downloadApp(from: sourceURL) { result in guard let storeApp = self.app as? StoreApp else {
return self.download(self.app)
}
// Verify storeApp
storeApp.managedObjectContext?.perform {
do
{
let latestVersion = try self.verify(storeApp)
self.download(latestVersion)
}
catch let error as VerificationError where error.code == .iOSVersionNotSupported
{
guard let presentingViewController = self.context.presentingViewController,
let latestSupportedVersion = storeApp.latestSupportedVersion, case let version = latestSupportedVersion.version, version != storeApp.installedApp?.version
else { return self.finish(.failure(error)) }
let title = NSLocalizedString("Unsupported iOS Version", comment: "")
let message = error.localizedDescription + "\n\n" + NSLocalizedString("Would you like to download the last version compatible with this device instead?", comment: "")
DispatchQueue.main.async {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: UIAlertAction.cancel.title, style: UIAlertAction.cancel.style) { _ in
self.finish(.failure(OperationError.cancelled))
})
alertController.addAction(UIAlertAction(title: String(format: NSLocalizedString("Download %@ %@", comment: ""), self.appName, version), style: .default) { _ in
self.download(latestSupportedVersion)
})
presentingViewController.present(alertController, animated: true)
}
}
catch
{
self.finish(.failure(error))
}
}
}
override func finish(_ result: Result<ALTApplication, Error>)
{
do
{
try FileManager.default.removeItem(at: self.temporaryDirectory)
}
catch
{
print("Failed to remove DownloadAppOperation temporary directory: \(self.temporaryDirectory).", error)
}
super.finish(result)
}
}
private extension DownloadAppOperation
{
func verify(_ storeApp: StoreApp) throws -> AppVersion
{
guard let version = storeApp.latestAvailableVersion else {
let failureReason = String(format: NSLocalizedString("The latest version of %@ could not be determined.", comment: ""), self.appName)
throw OperationError.unknown(failureReason: failureReason)
}
if let minOSVersion = version.minOSVersion, !ProcessInfo.processInfo.isOperatingSystemAtLeast(minOSVersion)
{
throw VerificationError.iOSVersionNotSupported(app: storeApp, requiredOSVersion: minOSVersion)
}
else if let maxOSVersion = version.maxOSVersion, ProcessInfo.processInfo.operatingSystemVersion > maxOSVersion
{
throw VerificationError.iOSVersionNotSupported(app: storeApp, requiredOSVersion: maxOSVersion)
}
return version
}
func download(@Managed _ app: AppProtocol)
{
guard let sourceURL = $app.url else { return self.finish(.failure(OperationError.appNotFound(name: self.appName))) }
self.downloadIPA(from: sourceURL) { result in
do do
{ {
let application = try result.get() let application = try result.get()
@@ -97,24 +174,7 @@ class DownloadAppOperation: ResultOperation<ALTApplication>
} }
} }
override func finish(_ result: Result<ALTApplication, Error>) func downloadIPA(from sourceURL: URL, completionHandler: @escaping (Result<ALTApplication, Error>) -> Void)
{
do
{
try FileManager.default.removeItem(at: self.temporaryDirectory)
}
catch
{
print("Failed to remove DownloadAppOperation temporary directory: \(self.temporaryDirectory).", error)
}
super.finish(result)
}
}
private extension DownloadAppOperation
{
func downloadApp(from sourceURL: URL, completionHandler: @escaping (Result<ALTApplication, Error>) -> Void)
{ {
func finishOperation(_ result: Result<URL, Error>) func finishOperation(_ result: Result<URL, Error>)
{ {

View File

@@ -25,7 +25,10 @@ extension VerificationError
static func privateEntitlements(_ entitlements: [String: Any], app: ALTApplication) -> VerificationError { VerificationError(code: .privateEntitlements, app: app, entitlements: entitlements) } static func privateEntitlements(_ entitlements: [String: Any], app: ALTApplication) -> VerificationError { VerificationError(code: .privateEntitlements, app: app, entitlements: entitlements) }
static func mismatchedBundleIdentifiers(sourceBundleID: String, app: ALTApplication) -> VerificationError { VerificationError(code: .mismatchedBundleIdentifiers, app: app, sourceBundleID: sourceBundleID) } static func mismatchedBundleIdentifiers(sourceBundleID: String, app: ALTApplication) -> VerificationError { VerificationError(code: .mismatchedBundleIdentifiers, app: app, sourceBundleID: sourceBundleID) }
static func iOSVersionNotSupported(app: ALTApplication) -> VerificationError { VerificationError(code: .iOSVersionNotSupported, app: app) }
static func iOSVersionNotSupported(app: AppProtocol, osVersion: OperatingSystemVersion = ProcessInfo.processInfo.operatingSystemVersion, requiredOSVersion: OperatingSystemVersion?) -> VerificationError {
VerificationError(code: .iOSVersionNotSupported, app: app, deviceOSVersion: osVersion, requiredOSVersion: requiredOSVersion)
}
} }
struct VerificationError: ALTLocalizedError struct VerificationError: ALTLocalizedError
@@ -35,22 +38,44 @@ struct VerificationError: ALTLocalizedError
var errorTitle: String? var errorTitle: String?
var errorFailure: String? var errorFailure: String?
var app: ALTApplication? @Managed var app: AppProtocol?
var entitlements: [String: Any]? var entitlements: [String: Any]?
var sourceBundleID: String? var sourceBundleID: String?
var deviceOSVersion: OperatingSystemVersion?
var requiredOSVersion: OperatingSystemVersion?
var errorDescription: String? {
switch self.code
{
case .iOSVersionNotSupported:
guard let deviceOSVersion else { return nil }
var failureReason = self.errorFailureReason
if self.app == nil
{
// failureReason does not start with app name, so make first letter lowercase.
let firstLetter = failureReason.prefix(1).lowercased()
failureReason = firstLetter + failureReason.dropFirst()
}
let localizedDescription = String(format: NSLocalizedString("This device is running iOS %@, but %@", comment: ""), deviceOSVersion.stringValue, failureReason)
return localizedDescription
default: return nil
}
}
var errorFailureReason: String { var errorFailureReason: String {
switch self.code switch self.code
{ {
case .privateEntitlements: case .privateEntitlements:
let appName = (self.app?.name as String?).map { String(format: NSLocalizedString("“%@”", comment: ""), $0) } ?? NSLocalizedString("The app", comment: "") let appName = self.$app.name ?? NSLocalizedString("The app", comment: "")
return String(format: NSLocalizedString("%@ requires private permissions.", comment: ""), appName) return String(format: NSLocalizedString("%@ requires private permissions.", comment: ""), appName)
case .mismatchedBundleIdentifiers: case .mismatchedBundleIdentifiers:
if let app = self.app, let bundleID = self.sourceBundleID if let appBundleID = self.$app.bundleIdentifier, let bundleID = self.sourceBundleID
{ {
return String(format: NSLocalizedString("The bundle ID “%@” does not match the one specified by the source (“%@”).", comment: ""), app.bundleIdentifier, bundleID) return String(format: NSLocalizedString("The bundle ID “%@” does not match the one specified by the source (“%@”).", comment: ""), appBundleID, bundleID)
} }
else else
{ {
@@ -58,22 +83,25 @@ struct VerificationError: ALTLocalizedError
} }
case .iOSVersionNotSupported: case .iOSVersionNotSupported:
if let app = self.app let appName = self.$app.name ?? NSLocalizedString("The app", comment: "")
let deviceOSVersion = self.deviceOSVersion ?? ProcessInfo.processInfo.operatingSystemVersion
guard let requiredOSVersion else {
return String(format: NSLocalizedString("%@ does not support iOS %@.", comment: ""), appName, deviceOSVersion.stringValue)
}
if deviceOSVersion > requiredOSVersion
{ {
var version = "iOS \(app.minimumiOSVersion.majorVersion).\(app.minimumiOSVersion.minorVersion)" // Device OS version is higher than maximum supported OS version.
if app.minimumiOSVersion.patchVersion > 0
{
version += ".\(app.minimumiOSVersion.patchVersion)"
}
let failureReason = String(format: NSLocalizedString("%@ requires %@.", comment: ""), app.name, version) let failureReason = String(format: NSLocalizedString("%@ requires iOS %@ or earlier.", comment: ""), appName, requiredOSVersion.stringValue)
return failureReason return failureReason
} }
else else
{ {
let version = ProcessInfo.processInfo.operatingSystemVersion.stringValue // Device OS version is lower than minimum supported OS version.
let failureReason = String(format: NSLocalizedString("This app does not support iOS %@.", comment: ""), version) let failureReason = String(format: NSLocalizedString("%@ requires iOS %@ or later.", comment: ""), appName, requiredOSVersion.stringValue)
return failureReason return failureReason
} }
} }
@@ -114,7 +142,7 @@ class VerifyAppOperation: ResultOperation<Void>
} }
guard ProcessInfo.processInfo.isOperatingSystemAtLeast(app.minimumiOSVersion) else { guard ProcessInfo.processInfo.isOperatingSystemAtLeast(app.minimumiOSVersion) else {
throw VerificationError.iOSVersionNotSupported(app: app) throw VerificationError.iOSVersionNotSupported(app: app, requiredOSVersion: app.minimumiOSVersion)
} }
if #available(iOS 13.5, *) if #available(iOS 13.5, *)
@@ -196,8 +224,7 @@ private extension VerifyAppOperation
})) }))
presentingViewController.present(alertController, animated: true, completion: nil) presentingViewController.present(alertController, animated: true, completion: nil)
case .mismatchedBundleIdentifiers: return completion(.failure(error)) case .mismatchedBundleIdentifiers, .iOSVersionNotSupported: return completion(.failure(error))
case .iOSVersionNotSupported: return completion(.failure(error))
} }
} }
} }