私のアプリケーションでは、ユーザーは 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;
}
}
}
}
}
どういうわけか、復元購入が機能していないようです..サンドボックスユーザーを使用してテストしています。何をすべきか提案はありますか?アイテムを購入することは問題なく機能しますが、アイテムを購入した同じユーザーで購入を復元しようとすると、機能しません。私のコードに何か問題がありますか、それとも何か違うことをしますか?