|
|
@@ -1,4 +1,5 @@
|
|
|
import Foundation
|
|
|
+import StoreKit
|
|
|
|
|
|
extension Error {
|
|
|
/// User-facing text for alerts and labels, respecting Settings → Language.
|
|
|
@@ -6,6 +7,13 @@ extension Error {
|
|
|
if let localizedError = self as? LocalizedError,
|
|
|
let description = localizedError.errorDescription,
|
|
|
description.isEmpty == false {
|
|
|
+ let fromAppBundle = description.localized
|
|
|
+ if fromAppBundle != description {
|
|
|
+ return fromAppBundle
|
|
|
+ }
|
|
|
+ if Self.isFrameworkLocalizedError(self) {
|
|
|
+ return Self.genericUserFacingMessage
|
|
|
+ }
|
|
|
return description
|
|
|
}
|
|
|
|
|
|
@@ -17,14 +25,14 @@ extension Error {
|
|
|
if fromAppBundle != description {
|
|
|
return fromAppBundle
|
|
|
}
|
|
|
- if Self.isSystemLocalizedDomain(nsError.domain) {
|
|
|
- return "An issue occurred. Please try again.".localized
|
|
|
+ if Self.isFrameworkLocalizedError(self) {
|
|
|
+ return Self.genericUserFacingMessage
|
|
|
}
|
|
|
return description
|
|
|
}
|
|
|
|
|
|
- if Self.isSystemLocalizedDomain(nsError.domain) {
|
|
|
- return "An issue occurred. Please try again.".localized
|
|
|
+ if Self.isFrameworkLocalizedError(self) {
|
|
|
+ return Self.genericUserFacingMessage
|
|
|
}
|
|
|
|
|
|
let systemDescription = localizedDescription
|
|
|
@@ -32,7 +40,42 @@ extension Error {
|
|
|
return systemDescription
|
|
|
}
|
|
|
|
|
|
- return "An issue occurred. Please try again.".localized
|
|
|
+ return Self.genericUserFacingMessage
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Localized purchase/restore failure copy that never surfaces raw StoreKit or system English.
|
|
|
+ var localizedPurchaseFailureMessage: String {
|
|
|
+ if Self.isFrameworkLocalizedError(self) {
|
|
|
+ return Self.purchaseFailureMessage
|
|
|
+ }
|
|
|
+
|
|
|
+ let message = localizedUserFacingMessage
|
|
|
+ let nsError = self as NSError
|
|
|
+ if message == nsError.localizedDescription
|
|
|
+ || message == (self as? LocalizedError)?.errorDescription {
|
|
|
+ return Self.purchaseFailureMessage
|
|
|
+ }
|
|
|
+ return message
|
|
|
+ }
|
|
|
+
|
|
|
+ private static var genericUserFacingMessage: String {
|
|
|
+ "An issue occurred. Please try again.".localized
|
|
|
+ }
|
|
|
+
|
|
|
+ private static var purchaseFailureMessage: String {
|
|
|
+ "Your purchase could not be completed. Please try again.".localized
|
|
|
+ }
|
|
|
+
|
|
|
+ private static func isFrameworkLocalizedError(_ error: Error) -> Bool {
|
|
|
+ if error is StoreKitError {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ let nsError = error as NSError
|
|
|
+ if isSystemLocalizedDomain(nsError.domain) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return nsError.domain.contains("StoreKit")
|
|
|
}
|
|
|
|
|
|
private static func isSystemLocalizedDomain(_ domain: String) -> Bool {
|