在我的应用程序中,用户可以购买PRO会员资格以解锁内容。
以下是用户购买PRO会员资格的视图控制器代码:
import UIKit
import StoreKit
class UpgradePROViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
@IBOutlet weak var upgradeButton: UIButton!
var ApplicationLoadTime = NSUserDefaults().integerForKey("isPRO")
var product_id: NSString?;
let loadedValue = NSUserDefaults().integerForKey("isPRO")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var ApplicationLoadTimeUserDefault = NSUserDefaults.standardUserDefaults()
if (ApplicationLoadTimeUserDefault.valueForKey("isPRO") != nil){
ApplicationLoadTime = ApplicationLoadTimeUserDefault.valueForKey("isPRO") as! NSInteger!
}
upgradeButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
upgradeButton.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Highlighted)
upgradeButton.layer.cornerRadius = 5
upgradeButton.layer.borderWidth = 1
upgradeButton.layer.borderColor = UIColor(white: 1.0, alpha: 0.35).CGColor
upgradeButton.layer.backgroundColor = UIColor(white: 0.1, alpha: 0.85).CGColor
product_id = "com.myself.MyApp.GetPRO";
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func upgradeButtonAction(sender: AnyObject) {
buyConsumable()
}
@IBAction func restoreButtonAction(sender: AnyObject) {
if (SKPaymentQueue.canMakePayments()){
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
println("Purchased Restored");
}
}
func buyConsumable(){
println("About to fetch the products");
// We check that we are allow to make the purchase.
if (SKPaymentQueue.canMakePayments())
{
var productID:NSSet = NSSet(object: self.product_id!);
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>);
productsRequest.delegate = self;
productsRequest.start();
println("Fething Products");
}else{
println("can't make purchases");
}
}
// Helper Methods
func buyProduct(product: SKProduct){
println("Sending the Payment Request to Apple");
var payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);
}
// Delegate Methods for IAP
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
println("got the request from Apple")
var count : Int = response.products.count
if (count>0) {
var validProducts = response.products
var validProduct: SKProduct = response.products[0] as! SKProduct
if (validProduct.productIdentifier == self.product_id) {
println(validProduct.localizedTitle)
println(validProduct.localizedDescription)
println(validProduct.price)
buyProduct(validProduct);
} else {
println(validProduct.productIdentifier)
}
} else {
println("nothing")
}
}
func request(request: SKRequest!, didFailWithError error: NSError!) {
println("La vaina fallo");
}
func unlockProFeatures(){
ApplicationLoadTime = 1
NSString(format: "isPRO", "%i", 1)
var ApplicationLoadTimeUserDefault = NSUserDefaults.standardUserDefaults()
ApplicationLoadTimeUserDefault.setValue(ApplicationLoadTime, forKey: "isPRO")
ApplicationLoadTimeUserDefault.synchronize()
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
println("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Purchased:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
println("Product Purchased");
unlockProFeatures()
break;
case .Failed:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
println("Purchased Failed");
break;
case .Restored:
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
println("Purchased Restored");
break;
default:
break;
}
}
}
}
}
不知何故,看起来恢复购买不起作用..我正在使用沙盒用户进行测试。有人有什么建议吗?它可以很好地购买物品,但是当我尝试使用已购买物品的同一用户恢复购买时,它不起作用。我的代码有什么问题,或者你会做些什么不同?