NSError+ALTServerError.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // NSError+ALTServerError.m
  3. // AltStore
  4. //
  5. // Created by Riley Testut on 5/30/19.
  6. // Copyright © 2019 Riley Testut. All rights reserved.
  7. //
  8. #import "NSError+ALTServerError.h"
  9. NSErrorDomain const AltServerErrorDomain = @"com.rileytestut.AltServer";
  10. NSErrorDomain const AltServerInstallationErrorDomain = @"com.rileytestut.AltServer.Installation";
  11. NSErrorDomain const AltServerConnectionErrorDomain = @"com.rileytestut.AltServer.Connection";
  12. NSErrorUserInfoKey const ALTUnderlyingErrorDomainErrorKey = @"underlyingErrorDomain";
  13. NSErrorUserInfoKey const ALTUnderlyingErrorCodeErrorKey = @"underlyingErrorCode";
  14. NSErrorUserInfoKey const ALTProvisioningProfileBundleIDErrorKey = @"bundleIdentifier";
  15. NSErrorUserInfoKey const ALTAppNameErrorKey = @"appName";
  16. NSErrorUserInfoKey const ALTDeviceNameErrorKey = @"deviceName";
  17. @implementation NSError (ALTServerError)
  18. + (void)load
  19. {
  20. [NSError setUserInfoValueProviderForDomain:AltServerErrorDomain provider:^id _Nullable(NSError * _Nonnull error, NSErrorUserInfoKey _Nonnull userInfoKey) {
  21. if ([userInfoKey isEqualToString:NSLocalizedFailureReasonErrorKey])
  22. {
  23. return [error altserver_localizedFailureReason];
  24. }
  25. else if ([userInfoKey isEqualToString:NSLocalizedRecoverySuggestionErrorKey])
  26. {
  27. return [error altserver_localizedRecoverySuggestion];
  28. }
  29. return nil;
  30. }];
  31. [NSError setUserInfoValueProviderForDomain:AltServerConnectionErrorDomain provider:^id _Nullable(NSError * _Nonnull error, NSErrorUserInfoKey _Nonnull userInfoKey) {
  32. if ([userInfoKey isEqualToString:NSLocalizedDescriptionKey])
  33. {
  34. return [error altserver_connection_localizedDescription];
  35. }
  36. else if ([userInfoKey isEqualToString:NSLocalizedRecoverySuggestionErrorKey])
  37. {
  38. return [error altserver_connection_localizedRecoverySuggestion];
  39. }
  40. // else if ([userInfoKey isEqualToString:NSLocalizedFailureErrorKey])
  41. // {
  42. // return @"";
  43. // }
  44. return nil;
  45. }];
  46. }
  47. //- (nullable NSString *)altserver_localizedDescription
  48. //{
  49. // switch ((ALTServerError)self.code)
  50. // {
  51. // case ALTServerErrorUnderlyingError:
  52. // {
  53. // NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
  54. // return [underlyingError localizedDescription];
  55. // }
  56. //
  57. // default:
  58. // {
  59. // return [self altserver_localizedFailureReason];
  60. // }
  61. // }
  62. //}
  63. - (nullable NSString *)altserver_localizedFailureReason
  64. {
  65. switch ((ALTServerError)self.code)
  66. {
  67. case ALTServerErrorUnderlyingError:
  68. {
  69. NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
  70. if (underlyingError.localizedFailureReason != nil)
  71. {
  72. return underlyingError.localizedFailureReason;
  73. }
  74. NSString *underlyingErrorCode = self.userInfo[ALTUnderlyingErrorCodeErrorKey];
  75. if (underlyingErrorCode != nil)
  76. {
  77. return [NSString stringWithFormat:NSLocalizedString(@"Error code: %@", @""), underlyingErrorCode];
  78. }
  79. return nil;
  80. }
  81. case ALTServerErrorUnknown:
  82. return NSLocalizedString(@"An unknown error occured.", @"");
  83. case ALTServerErrorConnectionFailed:
  84. #if TARGET_OS_OSX
  85. return NSLocalizedString(@"There was an error connecting to the device.", @"");
  86. #else
  87. return NSLocalizedString(@"Could not connect to AltServer.", @"");
  88. #endif
  89. case ALTServerErrorLostConnection:
  90. return NSLocalizedString(@"Lost connection to AltServer.", @"");
  91. case ALTServerErrorDeviceNotFound:
  92. return NSLocalizedString(@"AltServer could not find this device.", @"");
  93. case ALTServerErrorDeviceWriteFailed:
  94. return NSLocalizedString(@"Failed to write app data to device.", @"");
  95. case ALTServerErrorInvalidRequest:
  96. return NSLocalizedString(@"AltServer received an invalid request.", @"");
  97. case ALTServerErrorInvalidResponse:
  98. return NSLocalizedString(@"AltServer sent an invalid response.", @"");
  99. case ALTServerErrorInvalidApp:
  100. return NSLocalizedString(@"The app is invalid.", @"");
  101. case ALTServerErrorInstallationFailed:
  102. return NSLocalizedString(@"An error occured while installing the app.", @"");
  103. case ALTServerErrorMaximumFreeAppLimitReached:
  104. return NSLocalizedString(@"Cannot activate more than 3 apps and app extensions.", @"");
  105. case ALTServerErrorUnsupportediOSVersion:
  106. return NSLocalizedString(@"Your device must be running iOS 12.2 or later to install AltStore.", @"");
  107. case ALTServerErrorUnknownRequest:
  108. return NSLocalizedString(@"AltServer does not support this request.", @"");
  109. case ALTServerErrorUnknownResponse:
  110. return NSLocalizedString(@"Received an unknown response from AltServer.", @"");
  111. case ALTServerErrorInvalidAnisetteData:
  112. return NSLocalizedString(@"The provided anisette data is invalid.", @"");
  113. case ALTServerErrorPluginNotFound:
  114. return NSLocalizedString(@"AltServer could not connect to Mail plug-in.", @"");
  115. case ALTServerErrorProfileNotFound:
  116. return [self profileErrorLocalizedDescriptionWithBaseDescription:NSLocalizedString(@"Could not find profile", "")];
  117. case ALTServerErrorAppDeletionFailed:
  118. return NSLocalizedString(@"An error occured while removing the app.", @"");
  119. case ALTServerErrorRequestedAppNotRunning:
  120. {
  121. NSString *appName = self.userInfo[ALTAppNameErrorKey] ?: NSLocalizedString(@"The requested app", @"");
  122. NSString *deviceName = self.userInfo[ALTDeviceNameErrorKey] ?: NSLocalizedString(@"the device", @"");
  123. return [NSString stringWithFormat:NSLocalizedString(@"%@ is not currently running on %@.", ""), appName, deviceName];
  124. }
  125. }
  126. }
  127. - (nullable NSString *)altserver_localizedRecoverySuggestion
  128. {
  129. switch ((ALTServerError)self.code)
  130. {
  131. case ALTServerErrorConnectionFailed:
  132. case ALTServerErrorDeviceNotFound:
  133. return NSLocalizedString(@"Make sure you have trusted this device with your computer and WiFi sync is enabled.", @"");
  134. case ALTServerErrorPluginNotFound:
  135. return NSLocalizedString(@"Make sure Mail is running and the plug-in is enabled in Mail's preferences.", @"");
  136. case ALTServerErrorMaximumFreeAppLimitReached:
  137. return NSLocalizedString(@"Make sure “Offload Unused Apps” is disabled in Settings > iTunes & App Stores, then install or delete all offloaded apps.", @"");
  138. case ALTServerErrorRequestedAppNotRunning:
  139. {
  140. NSString *deviceName = self.userInfo[ALTDeviceNameErrorKey] ?: NSLocalizedString(@"your device", @"");
  141. return [NSString stringWithFormat:NSLocalizedString(@"Make sure the app is running in the foreground on %@ then try again.", @""), deviceName];
  142. }
  143. default:
  144. return nil;
  145. }
  146. }
  147. - (NSString *)profileErrorLocalizedDescriptionWithBaseDescription:(NSString *)baseDescription
  148. {
  149. NSString *localizedDescription = nil;
  150. NSString *bundleID = self.userInfo[ALTProvisioningProfileBundleIDErrorKey];
  151. if (bundleID)
  152. {
  153. localizedDescription = [NSString stringWithFormat:@"%@ “%@”", baseDescription, bundleID];
  154. }
  155. else
  156. {
  157. localizedDescription = [NSString stringWithFormat:@"%@.", baseDescription];
  158. }
  159. return localizedDescription;
  160. }
  161. #pragma mark - AltServerConnectionErrorDomain -
  162. - (nullable NSString *)altserver_connection_localizedDescription
  163. {
  164. switch ((ALTServerConnectionError)self.code)
  165. {
  166. case ALTServerConnectionErrorUnknown:
  167. {
  168. NSString *underlyingErrorDomain = self.userInfo[ALTUnderlyingErrorDomainErrorKey];
  169. NSString *underlyingErrorCode = self.userInfo[ALTUnderlyingErrorCodeErrorKey];
  170. if (underlyingErrorDomain != nil && underlyingErrorCode != nil)
  171. {
  172. return [NSString stringWithFormat:NSLocalizedString(@"%@ error %@.", @""), underlyingErrorDomain, underlyingErrorCode];
  173. }
  174. else if (underlyingErrorCode != nil)
  175. {
  176. return [NSString stringWithFormat:NSLocalizedString(@"Connection error code: %@", @""), underlyingErrorCode];
  177. }
  178. return nil;
  179. }
  180. case ALTServerConnectionErrorDeviceLocked:
  181. {
  182. NSString *deviceName = self.userInfo[ALTDeviceNameErrorKey] ?: NSLocalizedString(@"The device", @"");
  183. return [NSString stringWithFormat:NSLocalizedString(@"%@ is currently locked.", @""), deviceName];
  184. }
  185. case ALTServerConnectionErrorInvalidRequest:
  186. {
  187. NSString *deviceName = self.userInfo[ALTDeviceNameErrorKey] ?: NSLocalizedString(@"The device", @"");
  188. return [NSString stringWithFormat:NSLocalizedString(@"%@ received an invalid request from AltServer.", @""), deviceName];
  189. }
  190. case ALTServerConnectionErrorInvalidResponse:
  191. {
  192. NSString *deviceName = self.userInfo[ALTDeviceNameErrorKey] ?: NSLocalizedString(@"the device", @"");
  193. return [NSString stringWithFormat:NSLocalizedString(@"AltServer received an invalid response from %@.", @""), deviceName];
  194. }
  195. case ALTServerConnectionErrorUSBMUXD:
  196. {
  197. return NSLocalizedString(@"A connection to usbmuxd could not be established.", @"");
  198. }
  199. case ALTServerConnectionErrorSSL:
  200. {
  201. NSString *deviceName = self.userInfo[ALTDeviceNameErrorKey] ?: NSLocalizedString(@"the device", @"");
  202. return [NSString stringWithFormat:NSLocalizedString(@"A secure connection between AltServer and %@ could not be established.", @""), deviceName];
  203. }
  204. case ALTServerConnectionErrorTimedOut:
  205. {
  206. NSString *deviceName = self.userInfo[ALTDeviceNameErrorKey] ?: NSLocalizedString(@"the device", @"");
  207. return [NSString stringWithFormat:NSLocalizedString(@"The connection to %@ timed out.", @""), deviceName];
  208. }
  209. }
  210. return nil;
  211. }
  212. - (nullable NSString *)altserver_connection_localizedRecoverySuggestion
  213. {
  214. switch ((ALTServerConnectionError)self.code)
  215. {
  216. case ALTServerConnectionErrorUnknown:
  217. {
  218. return nil;
  219. }
  220. case ALTServerConnectionErrorDeviceLocked:
  221. {
  222. return NSLocalizedString(@"Please unlock the device with your passcode and try again.", @"");
  223. }
  224. case ALTServerConnectionErrorInvalidRequest:
  225. {
  226. break;
  227. }
  228. case ALTServerConnectionErrorInvalidResponse:
  229. {
  230. break;
  231. }
  232. case ALTServerConnectionErrorUSBMUXD:
  233. {
  234. break;
  235. }
  236. case ALTServerConnectionErrorSSL:
  237. {
  238. break;
  239. }
  240. case ALTServerConnectionErrorTimedOut:
  241. {
  242. break;
  243. }
  244. }
  245. return nil;
  246. }
  247. @end