Skip to content

Commit 9974acf

Browse files
authored
Merge pull request #562 from bizz84/develop
0.16.1, watchOS and SPM fixes
2 parents 7c83154 + dfe5002 commit 9974acf

38 files changed

+372
-302
lines changed

Package.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
// swift-tools-version:4.2
1+
// swift-tools-version:5.0
22
import PackageDescription
33

44
let package = Package(
55
name: "SwiftyStoreKit",
6-
// platforms: [.iOS("8.0"), .macOS("10.10"), tvOS("9.0"), .watchOS("2.0")],
6+
platforms: [.iOS("8.0"), .macOS("10.10"), .tvOS("9.0"), .watchOS("6.2")],
77
products: [
8-
.library(name: "SwiftyStoreKit", targets: ["SwiftyStoreKit"])
8+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
9+
.library(
10+
name: "SwiftyStoreKit",
11+
targets: ["SwiftyStoreKit"]),
12+
],
13+
dependencies: [
14+
// Dependencies declare other packages that this package depends on.
15+
// .package(url: /* package url */, from: "1.0.0"),
916
],
1017
targets: [
18+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
19+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
1120
.target(
1221
name: "SwiftyStoreKit",
13-
path: "SwiftyStoreKit"
14-
)
22+
dependencies: []),
23+
.testTarget(
24+
name: "SwiftyStoreKitTests",
25+
dependencies: ["SwiftyStoreKit"]),
1526
]
1627
)
File renamed without changes.

SwiftyStoreKit/CompleteTransactionsController.swift renamed to Sources/SwiftyStoreKit/CompleteTransactionsController.swift

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,22 @@ import StoreKit
3535
#endif
3636

3737
// MARK: - Missing SKError on watchOS
38-
#if os(watchOS)
38+
#if os(watchOS) && swift(<5.3)
3939
public struct SKError: Error {
4040

41-
var Code: SKErrorCode = .unknown
42-
var _nsError: NSError?
41+
public typealias Code = SKErrorCode
4342

44-
static var unknown: SKErrorCode = .unknown
45-
static var paymentInvalid: SKErrorCode = .paymentInvalid
43+
let _nsError: NSError
44+
45+
init(_nsError: NSError) {
46+
self._nsError = _nsError
47+
}
48+
49+
var code: Code {
50+
return Code(rawValue: _nsError.code) ?? .unknown
51+
}
4652

53+
static var unknown: Code = .unknown
54+
static var paymentInvalid: Code = .paymentInvalid
4755
}
4856
#endif

SwiftyStoreKit/PaymentQueueController.swift renamed to Sources/SwiftyStoreKit/PaymentQueueController.swift

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Foundation
2626
import StoreKit
2727

2828
protocol TransactionController {
29-
29+
3030
/// Process the supplied transactions on a given queue.
3131
/// - parameter transactions: transactions to process
3232
/// - parameter paymentQueue: payment queue for finishing transactions
@@ -41,40 +41,42 @@ public enum TransactionResult {
4141
}
4242

4343
public protocol PaymentQueue: class {
44-
44+
4545
func add(_ observer: SKPaymentTransactionObserver)
4646
func remove(_ observer: SKPaymentTransactionObserver)
47-
47+
4848
func add(_ payment: SKPayment)
4949

5050
func start(_ downloads: [SKDownload])
5151
func pause(_ downloads: [SKDownload])
52-
#if os(watchOS)
53-
func resumeDownloads(_ downloads: [SKDownload])
54-
#else
5552
func resume(_ downloads: [SKDownload])
56-
#endif
5753
func cancel(_ downloads: [SKDownload])
5854

5955
func restoreCompletedTransactions(withApplicationUsername username: String?)
60-
56+
6157
func finishTransaction(_ transaction: SKPaymentTransaction)
6258
}
6359

64-
extension SKPaymentQueue: PaymentQueue { }
60+
extension SKPaymentQueue: PaymentQueue {
61+
#if os(watchOS) && swift(<5.3)
62+
public func resume(_ downloads: [SKDownload]) {
63+
resumeDownloads(downloads)
64+
}
65+
#endif
66+
}
6567

6668
extension SKPaymentTransaction {
67-
69+
6870
open override var debugDescription: String {
6971
let transactionId = transactionIdentifier ?? "null"
7072
return "productId: \(payment.productIdentifier), transactionId: \(transactionId), state: \(transactionState), date: \(String(describing: transactionDate))"
7173
}
7274
}
7375

7476
extension SKPaymentTransactionState: CustomDebugStringConvertible {
75-
77+
7678
public var debugDescription: String {
77-
79+
7880
switch self {
7981
case .purchasing: return "purchasing"
8082
case .purchased: return "purchased"
@@ -87,24 +89,24 @@ extension SKPaymentTransactionState: CustomDebugStringConvertible {
8789
}
8890

8991
class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
90-
92+
9193
private let paymentsController: PaymentsController
92-
94+
9395
private let restorePurchasesController: RestorePurchasesController
94-
96+
9597
private let completeTransactionsController: CompleteTransactionsController
96-
98+
9799
unowned let paymentQueue: PaymentQueue
98-
100+
99101
deinit {
100102
paymentQueue.remove(self)
101103
}
102-
104+
103105
init(paymentQueue: PaymentQueue = SKPaymentQueue.default(),
104106
paymentsController: PaymentsController = PaymentsController(),
105107
restorePurchasesController: RestorePurchasesController = RestorePurchasesController(),
106108
completeTransactionsController: CompleteTransactionsController = CompleteTransactionsController()) {
107-
109+
108110
self.paymentQueue = paymentQueue
109111
self.paymentsController = paymentsController
110112
self.restorePurchasesController = restorePurchasesController
@@ -118,53 +120,53 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
118120
let message = "SwiftyStoreKit.completeTransactions() must be called when the app launches."
119121
assert(completeTransactionsController.completeTransactions != nil, message)
120122
}
121-
123+
122124
func startPayment(_ payment: Payment) {
123125
assertCompleteTransactionsWasCalled()
124126

125127
let skPayment = SKMutablePayment(product: payment.product)
126128
skPayment.applicationUsername = payment.applicationUsername
127129
skPayment.quantity = payment.quantity
128130

129-
if #available(iOS 12.2, tvOS 12.2, OSX 10.14.4, *) {
131+
if #available(iOS 12.2, tvOS 12.2, OSX 10.14.4, watchOS 6.2, *) {
130132
if let discount = payment.paymentDiscount?.discount as? SKPaymentDiscount {
131133
skPayment.paymentDiscount = discount
132134
}
133135
}
134136

135-
#if os(iOS) || os(tvOS)
136-
if #available(iOS 8.3, *) {
137+
#if os(iOS) || os(tvOS) || os(watchOS)
138+
if #available(iOS 8.3, watchOS 6.2, *) {
137139
skPayment.simulatesAskToBuyInSandbox = payment.simulatesAskToBuyInSandbox
138140
}
139-
#endif
140-
141+
#endif
142+
141143
paymentQueue.add(skPayment)
142-
144+
143145
paymentsController.append(payment)
144146
}
145-
147+
146148
func restorePurchases(_ restorePurchases: RestorePurchases) {
147149
assertCompleteTransactionsWasCalled()
148-
150+
149151
if restorePurchasesController.restorePurchases != nil {
150152
return
151153
}
152-
154+
153155
paymentQueue.restoreCompletedTransactions(withApplicationUsername: restorePurchases.applicationUsername)
154-
156+
155157
restorePurchasesController.restorePurchases = restorePurchases
156158
}
157-
159+
158160
func completeTransactions(_ completeTransactions: CompleteTransactions) {
159-
161+
160162
guard completeTransactionsController.completeTransactions == nil else {
161163
print("SwiftyStoreKit.completeTransactions() should only be called once when the app launches. Ignoring this call")
162164
return
163165
}
164-
166+
165167
completeTransactionsController.completeTransactions = completeTransactions
166168
}
167-
169+
168170
func finishTransaction(_ transaction: PaymentTransaction) {
169171
guard let skTransaction = transaction as? SKPaymentTransaction else {
170172
print("Object is not a SKPaymentTransaction: \(transaction)")
@@ -181,22 +183,18 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
181183
}
182184

183185
func resume(_ downloads: [SKDownload]) {
184-
#if os(watchOS)
185-
paymentQueue.resumeDownloads(downloads)
186-
#else
187186
paymentQueue.resume(downloads)
188-
#endif
189187
}
190188
func cancel(_ downloads: [SKDownload]) {
191189
paymentQueue.cancel(downloads)
192190
}
193-
191+
194192
var shouldAddStorePaymentHandler: ShouldAddStorePaymentHandler?
195193
var updatedDownloadsHandler: UpdatedDownloadsHandler?
196-
194+
197195
// MARK: SKPaymentTransactionObserver
198196
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
199-
197+
200198
/*
201199
* Some notes about how requests are processed by SKPaymentQueue:
202200
*
@@ -221,39 +219,39 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
221219
var unhandledTransactions = transactions.filter { $0.transactionState != .purchasing }
222220

223221
if unhandledTransactions.count > 0 {
224-
222+
225223
unhandledTransactions = paymentsController.processTransactions(transactions, on: paymentQueue)
226-
224+
227225
unhandledTransactions = restorePurchasesController.processTransactions(unhandledTransactions, on: paymentQueue)
228-
226+
229227
unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue)
230-
228+
231229
if unhandledTransactions.count > 0 {
232230
let strings = unhandledTransactions.map { $0.debugDescription }.joined(separator: "\n")
233231
print("unhandledTransactions:\n\(strings)")
234232
}
235233
}
236234
}
237-
235+
238236
func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
239-
237+
240238
}
241-
239+
242240
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
243-
241+
244242
restorePurchasesController.restoreCompletedTransactionsFailed(withError: error)
245243
}
246-
244+
247245
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
248-
246+
249247
restorePurchasesController.restoreCompletedTransactionsFinished()
250248
}
251-
249+
252250
func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {
253-
251+
254252
updatedDownloadsHandler?(downloads)
255253
}
256-
254+
257255
#if os(iOS) && !targetEnvironment(macCatalyst)
258256
func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {
259257

File renamed without changes.

0 commit comments

Comments
 (0)