Ios 内购总是失败

Ios in app purchase always fails

我正在尝试在应用内购买非消耗品。我设置了所有方法,几天前它运行良好,但现在总是失败。

override func viewDidLoad() {
    super.viewDidLoad()

    //In-App Prurchase Transaction Observer 
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    self.getProductInfo() }

在我的 viewDidLoad 中,我添加了事务观察器,然后处理其他所有事情。

//In-App Prurchase
func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
    let products = response.products
    if products.count != 0{
        product = products[0] as! SKProduct
    }
}

//In-App Prurchase
func getProductInfo(){
    if SKPaymentQueue.canMakePayments(){
        let productID:NSSet = NSSet(object: "premiunAccountProductID")
        let request:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
        request.delegate = self
        request.start()
    }
}

//In-App Prurchase
@IBAction func goPremiunPressed(sender: AnyObject) {

    if IJReachability.isConnectedToNetwork(){//check for internet
        let payment:SKPayment = SKPayment(product: product)
            SKPaymentQueue.defaultQueue().addPayment(payment)
    } else{ // no internet connection
        let alertview = JSSAlertView()
        alertview.show(self, title: "Sorry", text: "You need to be connected to the internet in order to buy a premium account", color: UIColor(hex: "#e74c3c"), iconImage: UIImage(named: "Cancel"))
        alertview.setTextTheme(.Light)
    }
}


//In-App Prurchase
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
    for transaction:AnyObject in transactions{
        if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{

            switch trans.transactionState{
            case .Purchased:
                UserPreferences.userDefaults.setBool(true, forKey: "isPremium")
                UserPreferences.userDefaults.synchronize()
                println("Transanction Succeded. Is premium is now \(UserPreferences.isPremium)")
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break

            case .Failed:
                let alertview = JSSAlertView()
                alertview.show(self, title: "Sorry", text: "There was an error completing your transaction, please try again later.", color: UIColor(hex: "#e74c3c"), iconImage: UIImage(named: "Cancel"))
                alertview.setTextTheme(.Light)
                println("Transaction Failed")
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break

            case .Restored:
                SKPaymentQueue.defaultQueue().restoreCompletedTransactions()

            default:
                break
            }
        }

    }
}

所以每次我按下 "goPremium" 按钮时,都需要几秒钟,然后才转到 .Failed 案例。我试图与多个沙箱用户进行交易,但他们都失败了。

我使用了一个教程来编写所有这些代码,所以我并不真正了解这些代码中的一些正在做的所有事情,或者这是否是进行应用内购买的最有效方式。

有人可以告诉我我的代码是否有错误,或者是否有更好的方法来做到这一点。

谢谢!

我正在 运行ning Xcode 版本 6.4 (6E35b),这是一个模拟器错误。只需 运行 真实设备上的应用程序即可完美运行。