diff --git a/InAppFw.podspec b/InAppFw.podspec index a47b108..7bd447e 100644 --- a/InAppFw.podspec +++ b/InAppFw.podspec @@ -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 diff --git a/InAppFw/InAppFw.swift b/InAppFw/InAppFw.swift index 64614c0..76d6186 100644 --- a/InAppFw/InAppFw.swift +++ b/InAppFw/InAppFw.swift @@ -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() @@ -27,7 +41,6 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO public override init() { super.init() SKPaymentQueue.defaultQueue().addTransactionObserver(self) - //loadPurchasedProducts(true) productIdentifiers = Set() } @@ -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)") @@ -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!!!") } + } } } + } } @@ -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()) @@ -94,11 +106,9 @@ 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 { @@ -106,6 +116,8 @@ public class InAppFw: NSObject, SKProductsRequestDelegate, SKPaymentTransactionO } }) + task.resume() + } catch { print("validateReceipt: Caught error") } @@ -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...") @@ -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) } diff --git a/README.md b/README.md index 182be9d..bf0722d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -32,13 +38,13 @@ InAppFw.sharedInstance.addProductIds(ids: Set) * 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 @@ -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()