Skip to content

Commit

Permalink
Changed NSURLConnection to NSURLSession
Browse files Browse the repository at this point in the history
  • Loading branch information
Sándor Gyulai committed Oct 13, 2015
1 parent f565309 commit fc462b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 53 deletions.
29 changes: 1 addition & 28 deletions InAppFw.podspec
Original file line number Diff line number Diff line change
@@ -1,43 +1,16 @@
Pod::Spec.new do |s|

# 1
s.platform = :ios
s.ios.deployment_target = '8.0'
s.name = "InAppFw"
s.summary = "In App Purchase Manager framework for iOS"
s.requires_arc = true

# 2
s.version = "0.9.0"

# 3
s.version = "0.9.1"
s.license = { :type => "MIT", :file => "LICENSE" }

# 4 - Replace with your name and e-mail address
s.author = { "Sandor Gyulai" => "sandor.gyulai@icloud.com" }

# For example,
# s.author = { "Joshua Greene" => "jrg.developer@gmail.com" }


# 5 - Replace this URL with your own Github page's URL (from the address bar)
s.homepage = "https://github.com/sandorgyulai/InAppFramework"

# For example,
# s.homepage = "https://github.com/JRG-Developer/RWPickFlavor"


# 6 - Replace this URL with your own Git URL from "Quick Setup"
s.source = { :git => "https://github.com/sandorgyulai/InAppFramework.git", :tag => "#{s.version}"}

# For example,
# s.source = { :git => "https://github.com/JRG-Developer/RWPickFlavor.git", :tag => "#{s.version}"}


# 7
s.framework = "UIKit"

# 8
s.source_files = "InAppFw/**/*.{swift}"

end
54 changes: 33 additions & 21 deletions InAppFw/InAppFw.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
// The MIT License (MIT)
//
// InAppFw.swift
// InAppFw
// Copyright (c) 2015 Sándor Gyulai
//
// Created by Sándor Gyulai on 12/10/15.
// Copyright © 2015 Sándor Gyulai. All rights reserved.
// 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.

import UIKit
import StoreKit

public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver{

public let ProductPurchasedNotification = "IAPPurchasedNotification"
public let ProductPurchasedNotificationName = "IAPPurchasedNotification"

public static let sharedInstance = InAppFw()

Expand All @@ -27,7 +41,6 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
public override init() {
super.init()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
//loadPurchasedProducts(true)
productIdentifiers = Set<String>()
}

Expand All @@ -42,11 +55,13 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
public func loadPurchasedProducts(checkWithApple: Bool, completion: ((valid: Bool) -> Void)?) {

if let productIdentifiers = productIdentifiers {

for productIdentifier in productIdentifiers {

let isPurchased = NSUserDefaults.standardUserDefaults().boolForKey(productIdentifier)

if isPurchased {
purchasedProductIdentifiers.insert(productIdentifier)
print("Purchased: \(productIdentifier)")
} else {
print("Not purchased: \(productIdentifier)")
Expand All @@ -59,15 +74,12 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
if let completion = completion {
validateReceipt(false, completion: completion)
} else {
validateReceipt(false, completion: { (valid) -> Void in
if valid {
print("Receipt is Valid!")
} else {
print("BEWARE! Reciept is not Valid!!!")
}
})
validateReceipt(false) { (valid) -> Void in
if valid { print("Receipt is Valid!") } else { print("BEWARE! Reciept is not Valid!!!") }
}
}
}

}

}
Expand All @@ -80,7 +92,7 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
if let r = receipt {

let receiptData = r.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
let requestContent = ["receipt-data":receiptData]
let requestContent = [ "receipt-data" : receiptData ]

do {
let requestData = try NSJSONSerialization.dataWithJSONObject(requestContent, options: NSJSONWritingOptions())
Expand All @@ -94,18 +106,18 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
storeRequest.HTTPMethod = "POST"
storeRequest.HTTPBody = requestData

let queue = NSOperationQueue()

NSURLConnection.sendAsynchronousRequest(storeRequest, queue: queue, completionHandler: { (response, data, connectionError) -> Void in
if (connectionError != nil) {
print("Validation Error: \(connectionError)")
let task = NSURLSession.sharedSession().dataTaskWithRequest(storeRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print("Validation Error: \(error)")
self.hasValidReceipt = false
completion(valid: false)
} else {
self.checkStatus(data, completion: completion)
}
})

task.resume()

} catch {
print("validateReceipt: Caught error")
}
Expand Down Expand Up @@ -172,7 +184,7 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

//MARK: - Product Completions
//MARK: - Transactions

private func completeTransaction(transaction: SKPaymentTransaction) {
print("Complete Transaction...")
Expand Down Expand Up @@ -206,7 +218,7 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO
NSUserDefaults.standardUserDefaults().setBool(true, forKey: productIdentifier)
NSUserDefaults.standardUserDefaults().synchronize()

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

}

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# InAppFramework
In App Purchase Manager framework for iOS

##### ToDo

- ☐ Documentation!!
- ☑︎ Change NSURLConnection to NSURLSession
- ☐ New features...

### Installation

#### Cocoapods

```
pod 'InAppFw'
pod 'InAppFw'
```

### Usage
Expand All @@ -32,13 +38,13 @@ InAppFw.sharedInstance.addProductIds(ids: Set<String>)
* Request products from Apple

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

* Load the previously purchased products

```
InAppFw.sharedInstance.loadPurchasedProducts(checkWithApple: Bool, completion: ((valid: Bool) -> Void)?)
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
Expand All @@ -49,7 +55,7 @@ InAppFw.sharedInstance.loadPurchasedProducts(checkWithApple: Bool, completion: (
InAppFw.sharedInstance.purchaseProduct(product: SKProduct)
```

* Or Restore products purchased on an other device
* Or Restore products purchased on an other device

```
InAppFw.sharedInstance.restoreCompletedTransactions()
Expand Down

0 comments on commit fc462b5

Please sign in to comment.