Files
SwiftyStoreKit/README.md

361 lines
15 KiB
Markdown
Raw Normal View History

2015-09-03 18:58:51 +01:00
# SwiftyStoreKit
2016-07-22 09:48:30 +01:00
SwiftyStoreKit is a lightweight In App Purchases framework for iOS 8.0+, tvOS 9.0+ and OS X 10.10+, written in Swift.
2015-09-03 18:58:51 +01:00
2016-09-15 22:33:27 +01:00
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](http://mit-license.org)
[![Platform](http://img.shields.io/badge/platform-ios%20%7C%20osx-lightgrey.svg?style=flat)](https://developer.apple.com/resources/)
[![Language](http://img.shields.io/badge/language-swift-orange.svg?style=flat)](https://developer.apple.com/swift)
[![Build](https://img.shields.io/travis/bizz84/SwiftyStoreKit.svg?style=flat)](https://travis-ci.org/bizz84/SwiftyStoreKit)
[![Issues](https://img.shields.io/github/issues/bizz84/SwiftyStoreKit.svg?style=flat)](https://github.com/bizz84/SwiftyStoreKit/issues)
2015-12-26 01:29:28 +01:00
[![Cocoapod](http://img.shields.io/cocoapods/v/SwiftyStoreKit.svg?style=flat)](http://cocoadocs.org/docsets/SwiftyStoreKit/)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
2016-09-15 22:58:31 +01:00
[![Twitter](https://img.shields.io/badge/twitter-@biz84-blue.svg?maxAge=2592000)](http://twitter.com/biz84)
2016-09-13 08:18:48 +01:00
#### NOTE: Master branch now compiles with Swift 3.0 under Xcode 8.
#### Swift 2.2 / Xcode 7 compatibility is supported in the [swift-2.2 branch](https://github.com/bizz84/SwiftyStoreKit/tree/swift-2.2).
2015-12-26 01:29:28 +01:00
### Preview
2015-09-28 23:03:33 +01:00
<img src="https://github.com/bizz84/SwiftyStoreKit/raw/master/Screenshots/Preview.png" width="320">
<img src="https://github.com/bizz84/SwiftyStoreKit/raw/master/Screenshots/Preview2.png" width="320">
### Setup + Complete Transactions
Apple recommends to register a transaction observer [as soon as the app starts](https://developer.apple.com/library/ios/technotes/tn2387/_index.html):
> Adding your app's observer at launch ensures that it will persist during all launches of your app, thus allowing your app to receive all the payment queue notifications.
SwiftyStoreKit supports this by calling `completeTransactions()` when the app starts:
```swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
SwiftyStoreKit.completeTransactions() { completedTransactions in
for completedTransaction in completedTransactions {
if completedTransaction.transactionState == .Purchased || completedTransaction.transactionState == .Restored {
print("purchased: \(completedTransaction.productId)")
}
}
}
return true
}
```
If there are any pending transactions at this point, these will be reported by the completion block so that the app state and UI can be updated.
### Retrieve products info
```swift
SwiftyStoreKit.retrieveProductsInfo(["com.musevisions.SwiftyStoreKit.Purchase1"]) { result in
if let product = result.retrievedProducts.first {
2016-09-13 08:04:41 +01:00
let numberFormatter = NumberFormatter()
numberFormatter.locale = product.priceLocale
2016-09-13 08:04:41 +01:00
numberFormatter.numberStyle = .currency
let priceString = numberFormatter.string(from: product.price)
print("Product: \(product.localizedDescription), price: \(priceString)")
}
else if let invalidProductId = result.invalidProductIDs.first {
return alertWithTitle("Could not retrieve product info", message: "Invalid product identifier: \(invalidProductId)")
}
else {
print("Error: \(result.error)")
}
}
```
2015-09-04 12:04:10 +01:00
### Purchase a product
2015-09-03 18:58:51 +01:00
```swift
2015-09-26 10:53:20 +01:00
SwiftyStoreKit.purchaseProduct("com.musevisions.SwiftyStoreKit.Purchase1") { result in
2015-09-03 18:58:51 +01:00
switch result {
2016-07-22 09:48:30 +01:00
case .success(let productId):
2015-09-03 18:58:51 +01:00
print("Purchase Success: \(productId)")
2016-07-22 09:48:30 +01:00
case .error(let error):
2015-09-03 18:58:51 +01:00
print("Purchase Failed: \(error)")
}
}
```
2015-09-04 12:04:10 +01:00
### Restore previous purchases
2015-09-03 18:58:51 +01:00
```swift
SwiftyStoreKit.restorePurchases() { results in
if results.restoreFailedProducts.count > 0 {
print("Restore Failed: \(results.restoreFailedProducts)")
}
else if results.restoredProductIds.count > 0 {
print("Restore Success: \(results.restoredProductIds)")
}
else {
2015-09-03 18:58:51 +01:00
print("Nothing to Restore")
}
}
```
2015-09-04 11:05:00 +01:00
### Verify Receipt
2016-03-30 22:47:01 +02:00
```swift
SwiftyStoreKit.verifyReceipt() { result in
2016-07-22 09:48:30 +01:00
if case .error(let error) = result {
if case .noReceiptData = error {
self.refreshReceipt()
}
}
}
func refreshReceipt() {
SwiftyStoreKit.refreshReceipt { result in
switch result {
2016-07-22 09:48:30 +01:00
case .success:
print("Receipt refresh success")
2016-07-22 09:48:30 +01:00
case .error(let error):
print("Receipt refresh failed: \(error)")
}
}
}
```
2016-07-11 21:49:24 +01:00
When using auto-renewable subscriptions, the shared secret can be specified like so:
```
SwiftyStoreKit.verifyReceipt(password: "your_shared_secret")
```
### Verify Purchase
2016-05-18 18:31:17 +07:00
```swift
SwiftyStoreKit.verifyReceipt() { result in
switch result {
2016-07-22 09:48:30 +01:00
case .success(let receipt):
2016-05-24 11:57:31 +07:00
// Verify the purchase of Consumable or NonConsumable
2016-05-18 18:31:17 +07:00
let purchaseResult = SwiftyStoreKit.verifyPurchase(
productId: "com.musevisions.SwiftyStoreKit.Purchase1",
2016-05-23 18:46:23 +07:00
inReceipt: receipt
2016-05-18 18:31:17 +07:00
)
switch purchaseResult {
2016-07-22 09:48:30 +01:00
case .purchased(let expiresDate):
2016-05-18 18:31:17 +07:00
print("Product is purchased.")
2016-07-22 09:48:30 +01:00
case .notPurchased:
2016-05-23 18:46:23 +07:00
print("The user has never purchased this product")
}
case .Error(let error):
print("Receipt verification failed: \(error)")
}
}
```
Note that for consumable products, the receipt will only include the information for a couples of minutes after the purchase.
### Verify Subscription
```swift
SwiftyStoreKit.verifyReceipt() { result in
switch result {
2016-07-22 09:48:30 +01:00
case .success(let receipt):
// Verify the purchase of a Subscription
2016-05-24 11:57:31 +07:00
let purchaseResult = SwiftyStoreKit.verifySubscription(
productId: "com.musevisions.SwiftyStoreKit.Subscription",
2016-05-24 11:57:31 +07:00
inReceipt: receipt,
validUntil: NSDate()
validDuration: 3600 * 24 * 30, // Non Renewing Subscription only
2016-05-24 11:57:31 +07:00
)
switch purchaseResult {
2016-07-22 09:48:30 +01:00
case .purchased(let expiresDate):
2016-05-24 11:57:31 +07:00
print("Product is valid until \(expiresDate)")
2016-07-22 09:48:30 +01:00
case .expired(let expiresDate):
2016-05-24 11:57:31 +07:00
print("Product is expired since \(expiresDate)")
2016-07-22 09:48:30 +01:00
case .notPurchased:
2016-05-24 11:57:31 +07:00
print("The user has never purchased this product")
}
2016-06-04 21:48:49 +01:00
2016-07-22 09:48:30 +01:00
case .error(let error):
2016-05-18 18:31:17 +07:00
print("Receipt verification failed: \(error)")
}
}
```
2016-05-24 11:57:31 +07:00
To test the expiration of a Non Renewing Subscription, you must indicate the `validDuration` time interval in seconds.
2016-05-24 11:57:31 +07:00
2015-09-04 12:59:55 +01:00
**NOTE**:
The framework provides a simple block based API with robust error handling on top of the existing StoreKit framework. It does **NOT** persist in app purchases data locally. It is up to clients to do this with a storage solution of choice (i.e. NSUserDefaults, CoreData, Keychain).
2015-09-04 12:07:52 +01:00
## Installation
### CocoaPods
SwiftyStoreKit can be installed as a [CocoaPod](https://cocoapods.org/) and builds as a Swift framework. To install, include this in your Podfile.
2016-03-30 22:47:01 +02:00
```ruby
use_frameworks!
pod 'SwiftyStoreKit'
```
Once installed, just ```import SwiftyStoreKit``` in your classes and you're good to go.
### Carthage
To integrate SwiftyStoreKit into your Xcode project using [Carthage](https://github.com/Carthage/Carthage), specify it in your Cartfile:
2016-03-30 22:47:01 +02:00
```ogdl
github "bizz84/SwiftyStoreKit"
```
**NOTE**: Please ensure that you have the [latest](https://github.com/Carthage/Carthage/releases) Carthage installed.
2016-09-13 08:18:48 +01:00
## Swift 2.x / 3.x
2016-09-13 08:18:48 +01:00
Following the release of Xcode 8 GM:
2016-09-13 08:18:48 +01:00
* The old Swift 2.2 code has been moved to the `swift-2.2` branch
* The `swift-3.0` branch has been merged into `master`
As for versioning:
* Swift 3.0 work will be tagged as version `0.5.x`
* Swift 2.2 work will be tagged as version `0.3.x`
## Sample Code
The project includes demo apps [for iOS](https://github.com/bizz84/SwiftyStoreKit/blob/master/SwiftyStoreDemo/ViewController.swift) [and OSX](https://github.com/bizz84/SwiftyStoreKit/blob/master/SwiftyStoreOSXDemo/ViewController.swift) showing how to use SwiftyStoreKit.
Note that the pre-registered in app purchases in the demo apps are for illustration purposes only and may not work as iTunes Connect may invalidate them.
2015-09-04 12:07:52 +01:00
#### Features
2015-09-04 12:09:35 +01:00
- Super easy to use block based API
- Support for consumable, non-consumable in-app purchases
- Support for free, auto renewable and non renewing subscriptions
2015-09-04 12:04:10 +01:00
- Receipt verification
2016-06-07 08:46:05 +01:00
- iOS, tvOS and OS X compatible
- enum-based error handling
2015-09-04 12:04:10 +01:00
## Known issues
#### Requests lifecycle
While SwiftyStoreKit tries handle concurrent purchase or restore purchases requests, it is not guaranteed that this will always work flawlessly.
This is in part because using a closure-based API does not map perfectly well with the lifecycle of payments in `SKPaymentQueue`.
In real applications the following could happen:
1. User starts a purchase
2. User kills the app
3. OS continues processing this, resulting in a failed or successful purchase
4. App is restarted (payment queue is not updated yet)
5. User starts another purchase (the old transaction may interfere with the new purchase)
To prevent situations like this from happening, a `completeTransactions()` method has been added in version 0.2.8. This should be called when the app starts as it can take care of clearing the payment queue and notifying the app of the transactions that have finished.
#### Multiple accounts
The user can background the hosting application and change the Apple ID used with the App Store, then foreground the app. This has been observed to cause problems with SwiftyStoreKit - other IAP implementations may suffer from this as well.
2016-04-24 21:51:45 +01:00
## Changelog
2016-09-13 08:18:48 +01:00
#### Version 0.5.0 (swift-3.0)
* Port SwiftyStoreKit to Swift 3.0, compiles under Xcode 8 GM
2016-07-22 09:48:30 +01:00
* Increased minimum deployment target for OS X to version 10.10
2016-09-13 08:18:48 +01:00
#### Version 0.3.0 (swift-2.2)
2016-07-11 21:49:24 +01:00
* Fixed the `verifyReceipt()` method to use production environment by default. This falls back to the test environment if the receipt is a sandbox receipt.
2016-06-07 08:46:05 +01:00
#### Version 0.2.10
* Added **tvOS** support
* Faster compilation time for `verifySubscription()` implementation
2016-05-26 08:21:00 +01:00
#### Version 0.2.9
* Added support for verifying purchases and subscriptions. This includes consumable and non consumable purchases, auto-renewing, free and non-renewing subscriptions.
* The `purchaseProduct()` now takes an optional `applicationUsername` string which can be used to help detect irregular activity on transactions. Read more about this on the [Apple docs](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Chapters/RequestPayment.html).
2016-05-26 08:21:00 +01:00
* Updated `verifyReceipt()` so that the completion block is called on the main thread.
#### Version 0.2.8
* Added `completeTransactions()` method to clear payment queue and return information about payments that have completed / failed.
2016-04-24 22:25:44 +01:00
#### Version 0.2.7
* Fixed **critical issue** that was causing the callbacks for `purchaseProduct()` and `restorePurchases()` to get mixed up when multiple requests were running concurrently. Related issues: [#3](https://github.com/bizz84/SwiftyStoreKit/issues/3), [#22](https://github.com/bizz84/SwiftyStoreKit/issues/22), [#26](https://github.com/bizz84/SwiftyStoreKit/issues/26). _Note that while code analysis and various testing scenarios indicate that this is now resolved, this has not yet been confirmed by the reporters of the issues._
#### Version 0.2.6
2016-04-24 21:51:45 +01:00
* Retrieve multiple products info at once. Introduces the new `retrieveProductsInfo()` API call, which takes a set of product IDs and returns a struct with information about the corresponding SKProducts. [Related issue #21](https://github.com/bizz84/SwiftyStoreKit/issues/21)
2016-04-24 22:25:44 +01:00
#### Version 0.2.5
2016-04-24 21:51:45 +01:00
2016-04-24 22:02:11 +01:00
* The `restorePurchases()` completion closure has been changed to return all restored purchases in one call. [Related issue #18](https://github.com/bizz84/SwiftyStoreKit/issues/18)
2016-04-24 21:51:45 +01:00
2016-04-24 22:25:44 +01:00
#### Version 0.2.4
2016-04-24 21:51:45 +01:00
* Carthage compatible
* Fixed Swift 2.2 warnings
2016-04-24 22:25:44 +01:00
#### Previous versions
2016-04-24 21:51:45 +01:00
* Receipt verification
* OS X support
2015-09-04 11:05:00 +01:00
## Implementation Details
In order to make a purchase, two operations are needed:
2015-09-04 11:05:00 +01:00
- Obtain the ```SKProduct``` corresponding to the productId that identifies the app purchase, via ```SKProductRequest```.
2016-06-04 21:48:49 +01:00
2015-09-04 11:49:43 +01:00
- Submit the payment for that product via ```SKPaymentQueue```.
2015-09-04 11:05:00 +01:00
The framework takes care of caching SKProducts so that future requests for the same ```SKProduct``` don't need to perform a new ```SKProductRequest```.
### Requesting products information
SwiftyStoreKit wraps the delegate-based ```SKProductRequest``` API with a block based class named ```InAppProductQueryRequest```, which returns a `RetrieveResults` value with information about the obtained products:
2015-09-04 11:05:00 +01:00
```swift
public struct RetrieveResults {
public let retrievedProducts: Set<SKProduct>
public let invalidProductIDs: Set<String>
public let error: NSError?
2015-09-04 11:05:00 +01:00
}
```
This value is then surfaced back to the caller of the `retrieveProductsInfo()` method the completion closure so that the client can update accordingly.
2015-09-04 11:05:00 +01:00
### Purchasing a product / Restoring purchases
`InAppProductPurchaseRequest` is a wrapper class for `SKPaymentQueue` that can be used to purchase a product or restore purchases.
2015-09-04 11:05:00 +01:00
The class conforms to the `SKPaymentTransactionObserver` protocol in order to receive transactions notifications from the payment queue. The following outcomes are defined for a purchase/restore action:
2015-09-04 11:05:00 +01:00
```swift
2015-09-04 11:49:43 +01:00
enum TransactionResult {
2016-07-22 09:48:30 +01:00
case purchased(productId: String)
case restored(productId: String)
case failed(error: NSError)
2015-09-04 11:05:00 +01:00
}
```
Depending on the operation, the completion closure for `InAppProductPurchaseRequest` is then mapped to either a `PurchaseResult` or a `RestoreResults` value and returned to the caller.
2015-09-04 11:05:00 +01:00
## Credits
Many thanks to [phimage](https://github.com/phimage) for adding OSX support and receipt verification.
## Apps using SwiftyStoreKit
It would be great to showcase apps using SwiftyStoreKit here. Pull requests welcome :)
2016-04-24 18:18:56 +03:00
* [MDacne](https://itunes.apple.com/app/id1044050208) - Acne analysis and treatment
* [Pixel Picker](https://itunes.apple.com/app/id930804327) - Image Color Picker
* [KType](https://itunes.apple.com/app/id1037000234) - Space shooter game
* [iPic](https://itunes.apple.com/app/id1101244278?ls=1&mt=12) - Automatically upload images and save Markdown links
* [iHosts](https://itunes.apple.com/app/id1102004240?ls=1&mt=12) - Perfect for editing /etc/hosts
2015-09-04 12:09:35 +01:00
## License
2015-09-04 11:05:00 +01:00
Copyright (c) 2015-2016 Andrea Bizzotto bizz84@gmail.com
2015-09-04 12:09:35 +01:00
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.