NoobProgrammer
NoobProgrammer

Reputation: 114

Ios in app purchase always fails

I'm trying to make a non consumable in app purchase. I have all my methods setup and it worked fine a couple of days ago, but now it always fails.

override func viewDidLoad() {
    super.viewDidLoad()

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

In my viewDidLoad I added the transaction observer, and then handled everything else.

//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
            }
        }

    }
}

So every time I press the "goPremium" button, it takes a couple of seconds and then just goes to the .Failed case. I have tried to make the transaction with multiple sandbox users and they all fail.

I used a tutorial to write all this code so I don't truly know about all the stuff that some of this code is doing and or if this is the most efficient way to make an in app purchase.

Can someone tell me if there is a mistake in my code or if there is a better way to do this.

Thanks!

Upvotes: 1

Views: 984

Answers (1)

NoobProgrammer
NoobProgrammer

Reputation: 114

I'm running Xcode Version 6.4 (6E35b) and this is a simulator error. Just run the application on a real device and it works perfectly.

Upvotes: 1

Related Questions