我试图在应用内购买非耗材。我已经设置了所有方法,并且它在几天前工作正常,但现在它总是失败。
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
}
}
}
}
每次我按下&#34; goPremium&#34;按钮,它需要几秒钟然后才进入.Failed案例。我曾尝试与多个沙盒用户进行交易,但都失败了。
我使用了一个教程来编写所有这些代码,因此我并不真正了解这些代码所做的所有事情,或者这是否是进行应用内购买的最有效方式。
有人可以告诉我代码中是否有错误,或者是否有更好的方法可以执行此操作。
谢谢!
答案 0 :(得分:1)
我正在运行Xcode版本6.4(6E35b),这是一个模拟器错误。只需在真实设备上运行该应用程序即可完美运行。