You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
StoreKitPlus is a Swift SDK that adds extra functionality for working with StoreKit 2, like extensions, observable state, services, etc.
21
+
StoreKitPlus is a Swift SDK that makes it easy to integrate with StoreKit 2.
22
22
23
-
StoreKitPlus has an observable ``StoreContext`` that lets you observe store state, service protocols and classes that let you fetch, purchase and sync products, as well as a ``ProductRepresentable`` protocol that lets you add a local product representation to your app.
23
+
StoreKitPlus has an observable ``StoreContext`` that lets you observe store state, services that let you fetch, purchase and sync products, as well as a ``ProductRepresentable`` protocol that lets you use local product representations in your app.
Copy file name to clipboardexpand all lines: Sources/StoreKitPlus/StoreKitPlus.docc/Getting-Started.md
+26-61
Original file line number
Diff line number
Diff line change
@@ -14,35 +14,21 @@ This article describes how you get started with StoreKitPlus.
14
14
}
15
15
16
16
17
-
## Services
18
-
19
17
StoreKitPlus has services and observable state that help you integrate with StoreKit.
20
18
21
-
*``StoreProductService`` can be implemented by types that can fetch StoreKit products.
22
-
*``StorePurchaseService`` can be implemented by types that can purchase StoreKit products.
23
-
*``StoreSyncService`` can be implemented by types that can sync purchases and products.
24
19
25
-
There is also a ``StoreService`` protocol that implements all these protocols, for when you want a single service to do everything.
26
20
27
-
``StandardStoreProductService``, ``StandardStorePurchaseService`` and ``StandardStoreService`` implement these protocols and can be subclassed to customize their behavior.
21
+
## Services
28
22
23
+
A ``StoreService`` can be used to integrate with StoreKit, such as fetching and purchasing products, restoring purchases, etc.
29
24
25
+
You can use the ``StandardStoreService`` as is, or subclass it to make modifications to any part of the StoreKit integration.
30
26
31
-
## Observable state
32
27
33
-
StoreKitPlus has an observable ``StoreContext`` that keeps track of the available and purchased products for an app. It can be injected into the store services to automatically be kept in sync.
34
28
35
-
For instance, injecting a context into a ``StandardStoreService`` and calling ``StoreSyncService/syncStoreData()`` will automatically write products and transactions to the context:
29
+
## Observable state
36
30
37
-
```swift
38
-
let productIds = ["com.your-app.productid"]
39
-
let context =StoreContext()
40
-
let service =StandardStoreService(
41
-
productIds: productIds,
42
-
context: context
43
-
)
44
-
tryawait service.syncStoreData()
45
-
```
31
+
StoreKitPlus has an observable ``StoreContext`` that keeps track of the available and purchased products for an app. It can be injected into the store service's functions to automatically keep it in sync.
46
32
47
33
Although StoreKit products and transactions are not codable, this context will persist products and purchases by their ID, which means that you can use local product representations to keep track of products and purchases in your app.
48
34
@@ -87,7 +73,6 @@ Since some operations require real StoreKit `Product` values, you should display
87
73
88
74
89
75
90
-
91
76
## Fetching products
92
77
93
78
You can use the native `Product` type to fetch products with StoreKit:
@@ -97,18 +82,15 @@ let productIds = ["com.your-app.productid"]
97
82
let products =tryawait Product.products(for: productIds)
98
83
```
99
84
100
-
You can also use a ``StoreProductService``, for instance this standard one:
85
+
You can also use a ``StoreService``, for instance this standard one:
101
86
102
87
```swift
103
-
let productIds = ["com.your-app.productid"]
104
-
let context =StoreContext()
105
-
let service =StandardStoreProductService(
106
-
productIds: productIds,
107
-
context: context)
88
+
let ids = ["com.your-app.productid"]
89
+
let service =StandardStoreService(productIds: ids)
108
90
let products =tryawait service.getProducts()
109
91
```
110
92
111
-
The standard service communicates with StoreKit and syncs the result with the provided context.
93
+
You can subclass ``StandardStoreService`` to customize the fetch operation.
112
94
113
95
114
96
@@ -127,32 +109,27 @@ switch result {
127
109
return result
128
110
```
129
111
130
-
However, purchases involve a bunch of steps and can become pretty complicated. To make things easier, you can also use a ``StorePurchaseService``, for instance this standard one:
112
+
Since purchases involve a bunch of steps and can become complicated, you can also use a ``StoreService``:
131
113
132
114
```swift
133
-
let productIds = ["com.your-app.productid"]
134
-
let context =StoreContext()
135
-
let service =StandardStorePurchaseService(productIds: productIds, context: context)
115
+
let ids = ["com.your-app.productid"]
116
+
let service =StandardStorePurchaseService(productIds: ids)
136
117
let result =tryawait service.purchase(product)
137
118
```
138
119
139
-
The standard service communicates with StoreKit and syncs the result with the provided context.
120
+
The ``StandardStoreService`` will automatically verify and finish the purchase transactions.
140
121
141
122
142
123
143
124
## Restoring purchases
144
125
145
126
You can use the native `StoreKit.Product` to verify transactions and see which that are purchased, not expired, not revoked etc.
146
127
147
-
However, this involves many steps and can become pretty complicated. To make things easier, you can also use a ``StorePurchaseService``, for instance this standard one:
128
+
However, this involves many steps and can become complicated. To make things easier, you can also use a ``StoreService``:
148
129
149
130
```swift
150
-
let productIds = ["com.your-app.productid"]
151
-
let context =StoreContext()
152
-
let service =StandardStorePurchaseService(
153
-
productIds: productIds,
154
-
context: context
155
-
)
131
+
let ids = ["com.your-app.productid"]
132
+
let service =StandardStorePurchaseService(productIds: ids)
156
133
tryawait service.restorePurchases()
157
134
```
158
135
@@ -164,16 +141,13 @@ The standard service communicates with StoreKit and syncs the result with the pr
164
141
165
142
To perform a product and purchase sync, you can fetch products and transactions from StoreKit.
166
143
167
-
However, this involves a bunch of steps and can become pretty complicated. To make things easier, you can also use a ``StoreSyncService``, for instance this standard one:
144
+
Using a ``StoreService`` lets you easily sync all data to a ``StoreContext``:
168
145
169
146
```swift
170
-
letproductIds= ["com.your-app.productid"]
147
+
letids= ["com.your-app.productid"]
171
148
let context =StoreContext()
172
-
let service =StandardStoreService(
173
-
productIds: productIds,
174
-
context: context
175
-
)
176
-
tryawait service.syncStoreData()
149
+
let service =StandardStoreService(productIds: ids)
150
+
tryawait service.syncStoreData(to: context)
177
151
```
178
152
179
153
The standard service communicates with StoreKit and syncs the result with the provided context.
@@ -189,7 +163,7 @@ To sync data when your app launches or becomes active, you can listen to its sce
189
163
structMyApp: App {
190
164
191
165
@StateObject
192
-
privatevarstoreContext=StoreContext()
166
+
privatevarcontext=StoreContext()
193
167
194
168
@Environment(\.scenePhase)
195
169
privatevar scenePhase
@@ -198,33 +172,24 @@ struct MyApp: App {
198
172
WindowGroup {
199
173
RootView()
200
174
.onChange(of: scenePhase, perform: syncStoreData)
201
-
.environmentObject(storeContext)
202
175
}
203
176
}
204
177
}
205
178
206
179
privateextensionMyApp {
207
180
208
-
// I use a service provider to resolve services, but you
209
-
// can create a service directly, like this:
210
-
var storeService: StoreService {
211
-
let productIds = ["com.your-app.productid"]
212
-
let service =StandardStoreService(
213
-
productIds: productIds,
214
-
context: storeContext
215
-
)
216
-
}
217
-
218
181
funcsyncStoreData(forphase: ScenePhase) {
219
-
guard phase == .active else { return
182
+
guard phase == .active else { return }
183
+
let ids = ["com.your-app.productid"]
184
+
let service =StandardStoreService(productIds: ids)
220
185
Task {
221
-
tryawait storeService.syncStoreData()
186
+
tryawait storeService.syncStoreData(to: context)
222
187
}
223
188
}
224
189
}
225
190
```
226
191
227
-
Since the standard implementations automatically sync changes to the provided context, injecting the context as an environment object will make the global state available to the entire app.
192
+
You can inject the contextas an `EnvironmentObject` to make the global state available to the entire app.
Copy file name to clipboardexpand all lines: Sources/StoreKitPlus/StoreKitPlus.docc/StoreKitPlus.md
+4-12
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,9 @@ StoreKitPlus is a Swift SDK that adds extra functionality for working with Store
8
8
9
9

10
10
11
-
StoreKitPlus is a Swift SDK that adds extra functionality for working with StoreKit 2, like extensions, observable state, services, etc.
11
+
StoreKitPlus is a Swift SDK that makes it easy to integrate with StoreKit 2.
12
12
13
-
StoreKitPlus has an observable ``StoreContext`` that lets you observe state, service protocols and classes that let you fetch, purchase and sync products, as well as a ``ProductRepresentable`` protocol that lets you add a local product representation to your app.
13
+
StoreKitPlus has an observable ``StoreContext`` that lets you observe store state, services that let you fetch, purchase and sync products, as well as a ``ProductRepresentable`` protocol that lets you use local product representations in your app.
14
14
15
15
16
16
@@ -53,22 +53,14 @@ StoreKitPlus is available under the MIT license.
0 commit comments