StoreManager
import Foundation
import UIKit
import StoreKit
class StoreManager:NSObject ,SKProductsRequestDelegate, SKPaymentTransactionObserver{
static let sharedInstance = StoreManager()
var transactionInProgress = false
var productIDs:Array<String!> = ["<App内課金の製品ID>"]
var productsArray: Array<SKProduct!> = []
var selectedProductIndex: Int!
var restore = false
var payment:SKPayment!
func requestProductInfo() {
if SKPaymentQueue.canMakePayments() {
let productIdentifiers = NSSet(array: StoreManager.sharedInstance.productIDs)
let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)
productRequest.delegate = self
productRequest.start()
}
else {
print("Cannot perform In App Purchases.")
}
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("productsRequest")
if response.products.count != 0 {
for product in response.products {
print("validProduct:\(product)")
StoreManager.sharedInstance.productsArray.append(product)
self.payment = SKPayment(product: product)
self.transactionInProgress = true
SKPaymentQueue.defaultQueue().addPayment(self.payment)
request.delegate = self
request.start()
}
}
else {
print("There are no products.")
}
if response.invalidProductIdentifiers.count != 0 {
print("invalidProductIdnetfiers:\(response.invalidProductIdentifiers.description)")
}
}
// 購入履歴が確認できた場合
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
var restore = false
print("paymentQueueRestore")
//すでに商品を購入していたら、復元を行う
for transaction in queue.transactions {
print(transaction.payment.productIdentifier)
if transaction.payment.productIdentifier == "Pro" {
restore = true
}
}
if restore == true {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "pro")
let av = UIAlertView(title: "", message: "Restored Successfully", delegate: nil, cancelButtonTitle: "OK")
av.show()
}else{
print("called")
// failed restore
let av = UIAlertView(title: "", message: "There is no item to restore", delegate: nil, cancelButtonTitle: "OK")
av.show()
}
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("update Transaction")
for transaction in transactions {
switch transaction.transactionState {
case SKPaymentTransactionState.Purchased:
print("Transaction completed successfully.")
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
self.transactionInProgress = false
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "pro")
NSUserDefaults.standardUserDefaults().synchronize()
case SKPaymentTransactionState.Failed:
print("Transaction Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
self.transactionInProgress = false
case SKPaymentTransactionState.Purchasing:
print("purchasing")
case SKPaymentTransactionState.Restored:
print("SKPaymentTransactionState.Restored")
self.transactionInProgress = false
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
default:
print("default")
}
}
}