From 07746174d4ded30be3fcbf9017a9c8e4e9a230af Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Fri, 13 Sep 2019 14:25:26 -0700 Subject: [PATCH] [AltServer] Improves error handling when installing apps --- AltKit/NSError+ALTServerError.h | 21 +++++++++++---------- AltKit/NSError+ALTServerError.m | 3 +++ AltServer/AppDelegate.swift | 16 +++++++++++++--- AltServer/Devices/ALTDeviceManager.mm | 17 ++++++++++++----- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/AltKit/NSError+ALTServerError.h b/AltKit/NSError+ALTServerError.h index 46b36fb0..f1d5c9c1 100644 --- a/AltKit/NSError+ALTServerError.h +++ b/AltKit/NSError+ALTServerError.h @@ -13,19 +13,20 @@ extern NSErrorDomain const AltServerInstallationErrorDomain; typedef NS_ERROR_ENUM(AltServerErrorDomain, ALTServerError) { - ALTServerErrorUnknown, - ALTServerErrorConnectionFailed, - ALTServerErrorLostConnection, + ALTServerErrorUnknown = 0, + ALTServerErrorConnectionFailed = 1, + ALTServerErrorLostConnection = 2, - ALTServerErrorDeviceNotFound, - ALTServerErrorDeviceWriteFailed, + ALTServerErrorDeviceNotFound = 3, + ALTServerErrorDeviceWriteFailed = 4, - ALTServerErrorInvalidRequest, - ALTServerErrorInvalidResponse, + ALTServerErrorInvalidRequest = 5, + ALTServerErrorInvalidResponse = 6, - ALTServerErrorInvalidApp, - ALTServerErrorInstallationFailed, - ALTServerErrorMaximumFreeAppLimitReached, + ALTServerErrorInvalidApp = 7, + ALTServerErrorInstallationFailed = 8, + ALTServerErrorMaximumFreeAppLimitReached = 9, + ALTServerErrorUnsupportediOSVersion = 10, }; NS_ASSUME_NONNULL_BEGIN diff --git a/AltKit/NSError+ALTServerError.m b/AltKit/NSError+ALTServerError.m index 64e71fe3..eb365a2d 100644 --- a/AltKit/NSError+ALTServerError.m +++ b/AltKit/NSError+ALTServerError.m @@ -58,6 +58,9 @@ NSErrorDomain const AltServerInstallationErrorDomain = @"com.rileytestut.AltServ case ALTServerErrorMaximumFreeAppLimitReached: return NSLocalizedString(@"You have reached the limit of 3 apps per device.", @""); + + case ALTServerErrorUnsupportediOSVersion: + return NSLocalizedString(@"Your device must be running iOS 12.2 or later to install AltStore.", @""); } } diff --git a/AltServer/AppDelegate.swift b/AltServer/AppDelegate.swift index 84bb6b13..841c56d4 100644 --- a/AltServer/AppDelegate.swift +++ b/AltServer/AppDelegate.swift @@ -137,13 +137,23 @@ private extension AppDelegate let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil) UNUserNotificationCenter.current().add(request) - case .failure(let error): + case .failure(let error as NSError): + let alert = NSAlert() + alert.alertStyle = .critical alert.messageText = NSLocalizedString("Installation Failed", comment: "") - alert.informativeText = error.localizedDescription + + if let underlyingError = error.userInfo[NSUnderlyingErrorKey] as? Error + { + alert.informativeText = underlyingError.localizedDescription + } + else + { + alert.informativeText = error.localizedDescription + } NSRunningApplication.current.activate(options: .activateIgnoringOtherApps) - + alert.runModal() } } diff --git a/AltServer/Devices/ALTDeviceManager.mm b/AltServer/Devices/ALTDeviceManager.mm index 309098fd..f856a183 100644 --- a/AltServer/Devices/ALTDeviceManager.mm +++ b/AltServer/Devices/ALTDeviceManager.mm @@ -622,12 +622,12 @@ void ALTDeviceManagerUpdateStatus(plist_t command, plist_t status, void *uuid) uint64_t code = 0; instproxy_status_get_error(status, &name, &description, &code); - if ((percent == -1 && progress.completedUnitCount > 0) || code != 0) + if ((percent == -1 && progress.completedUnitCount > 0) || code != 0 || name != NULL) { void (^completionHandler)(NSError *) = ALTDeviceManager.sharedManager.installationCompletionHandlers[UUID]; if (completionHandler != nil) { - if (code != 0) + if (code != 0 || name != NULL) { NSLog(@"Error installing app. %@ (%@). %@", @(code), @(name), @(description)); @@ -639,9 +639,16 @@ void ALTDeviceManagerUpdateStatus(plist_t command, plist_t status, void *uuid) } else { - NSError *underlyingError = [NSError errorWithDomain:AltServerInstallationErrorDomain code:code userInfo:@{NSLocalizedDescriptionKey: @(description)}]; - - error = [NSError errorWithDomain:AltServerErrorDomain code:ALTServerErrorInstallationFailed userInfo:@{NSUnderlyingErrorKey: underlyingError}]; + NSString *errorName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding]; + if ([errorName isEqualToString:@"DeviceOSVersionTooLow"]) + { + error = [NSError errorWithDomain:AltServerErrorDomain code:ALTServerErrorUnsupportediOSVersion userInfo:nil]; + } + else + { + NSError *underlyingError = [NSError errorWithDomain:AltServerInstallationErrorDomain code:code userInfo:@{NSLocalizedDescriptionKey: @(description)}]; + error = [NSError errorWithDomain:AltServerErrorDomain code:ALTServerErrorInstallationFailed userInfo:@{NSUnderlyingErrorKey: underlyingError}]; + } } completionHandler(error);