Skip to content

Commit

Permalink
Add purchase canceled notification
Browse files Browse the repository at this point in the history
  • Loading branch information
abelorosz committed Feb 19, 2016
1 parent e71314a commit 0158b26
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
2 changes: 1 addition & 1 deletion InAppFw.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Pod::Spec.new do |s|
s.name = "InAppFw"
s.summary = "In App Purchase Manager framework for iOS"
s.requires_arc = true
s.version = "0.9.2"
s.version = "0.9.3"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Sandor Gyulai" => "sandor.gyulai@icloud.com" }
s.homepage = "https://github.com/sandorgyulai/InAppFramework"
Expand Down
19 changes: 7 additions & 12 deletions InAppFw/InAppFw.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import UIKit
import StoreKit

let kIAPPurchasedNotification = "IAPPurchasedNotification"
let kIAPFailedNotification = "IAPFailedNotification"

public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver{

public let ProductPurchasedNotificationName = "IAPPurchasedNotification"

public static let sharedInstance = InAppFw()

var productIdentifiers: Set<String>?
Expand Down Expand Up @@ -185,7 +186,6 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
print("No productIdentifiers")
completionHandler(success: false, products: nil)
}

}

/**
Expand All @@ -203,10 +203,8 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
Check if the product with identifier is already purchased
*/
public func productPurchased(productIdentifier: String) -> (isPurchased: Bool, hasValidReceipt: Bool) {

let purchased = purchasedProductIdentifiers.contains(productIdentifier)
return (purchased, hasValidReceipt)

}

/**
Expand Down Expand Up @@ -235,23 +233,21 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
private func failedTransaction(transaction: SKPaymentTransaction) {
print("Failed Transaction...")

if (transaction.error!.code != SKErrorPaymentCancelled)
{
if (transaction.error!.code != SKErrorPaymentCancelled) {
print("Transaction error \(transaction.error!.code): \(transaction.error!.localizedDescription)")
}

SKPaymentQueue.defaultQueue().finishTransaction(transaction)
NSNotificationCenter.defaultCenter().postNotificationName(kIAPFailedNotification, object: nil, userInfo: nil)
}

private func provideContentForProductIdentifier(productIdentifier: String!) {

purchasedProductIdentifiers.insert(productIdentifier)

NSUserDefaults.standardUserDefaults().setBool(true, forKey: productIdentifier)
NSUserDefaults.standardUserDefaults().synchronize()

NSNotificationCenter.defaultCenter().postNotificationName(ProductPurchasedNotificationName, object: productIdentifier, userInfo: nil)

NSNotificationCenter.defaultCenter().postNotificationName(kIAPPurchasedNotification, object: productIdentifier, userInfo: nil)
}

// MARK: - Delegate Implementations
Expand All @@ -261,8 +257,7 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
{
if let trans = transaction as? SKPaymentTransaction
{
switch trans.transactionState
{
switch trans.transactionState {
case .Purchased:
completeTransaction(trans)
break
Expand Down
53 changes: 21 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,35 @@ pod 'InAppFw'

### Usage

* Register for Notifications with the name below where you want to do something

```
ProductPurchasedNotificationName
First you should add product IDs:
```swift
InAppFw.sharedInstance.addProductId(String)
InAppFw.sharedInstance.addProductIds([String])
```

* Add your product identifiers

Then you can request them from the Apple servers:
```swift
InAppFw.sharedInstance.requestProducts(completionHandler: (success: Bool, products: [SKProduct]?)
```
InAppFw.sharedInstance.addProductId(id: String)
```

or add multiple of them

Make purchases:
```swift
InAppFw.sharedInstance.purchaseProduct(SKProduct)
```
InAppFw.sharedInstance.addProductIds(ids: Set<String>)
```

* Request products from Apple

```
InAppFw.sharedInstance.requestProducts(completionHandler: (success:Bool, products:[SKProduct]?)
```

* Load the previously purchased products

Restore purchases:
```swift
InAppFw.sharedInstance.restoreCompletedTransactions()
```
InAppFw.sharedInstance.loadPurchasedProducts(checkWithApple: Bool, completion: ((valid: Bool) -> Void)?)
```

"checkWithApple" if true will validate the Purchase receipt with Apple Servers too. The completion will be only true if the receipt was valid

* Purchase product

Register for notifications:
```swift
- kIAPPurchasedNotification
- kIAPFailedNotification
```
InAppFw.sharedInstance.purchaseProduct(product: SKProduct)
```

* Or Restore products purchased on an other device

Load the previously purchased products:
```swift
InAppFw.sharedInstance.loadPurchasedProducts(checkWithApple: Bool, completion: ((valid: Bool) -> Void)?)
```
InAppFw.sharedInstance.restoreCompletedTransactions()
```
```checkWithApple```: if ```true```, will validate the Purchase receipt with Apple Servers too. The completion will be only true if the receipt is valid.

0 comments on commit 0158b26

Please sign in to comment.