overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() outRemoveAds.enabled = false if(SKPaymentQueue.canMakePayments()) { println("IAP is enabled,loading") var productID:NSSet = NSSet(objects: "com.webstart.tomsgamesinc.Startlight_No_Ads") var request: SKProductsRequest = SKProductsRequest(productIDentifIErs: productID as Set<NSObject>) request.delegate = self request.start() } else { println("please enable IAPS") } // Do any additional setup after loading the vIEw.}func buyProduct() { println("buy " + p.productIDentifIEr) var pay = SKPayment(product: p) SKPaymentQueue.defaultQueue().addTransactionObserver(self) SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)}//3func productsRequest(request: SKProductsRequest!,dIDReceiveResponse response: SKProductsResponse!) { println("product request") println(response.products) var myProduct = response.products for product in myProduct { println("product added") println(product.productIDentifIEr) println(product.localizedTitle) println(product.localizedDescription) println(product.price) List.append(product as! SKProduct) } outRemoveAds.enabled = true}// 4func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) { println("transactions restored") var purchasedItemIDS = [] for transaction in queue.transactions { var t: SKPaymentTransaction = transaction as! SKPaymentTransaction let prodID = t.payment.productIDentifIEr as String switch prodID { case "com.webstart.tomsgamesinc.Startlight_No_Ads": println("remove ads") removeAds() default: println("IAP not setup") } }}// 5func paymentQueue(queue: SKPaymentQueue!,updatedTransactions transactions: [AnyObject]!) { println("add paymnet") for transaction:AnyObject in transactions { var trans = transaction as! SKPaymentTransaction println(trans.error) switch trans.transactionState { case .Purchased: println("buy,ok unlock iap here") println(p.productIDentifIEr) let prodID = p.productIDentifIEr as String switch prodID { case "com.webstart.tomsgamesinc.Startlight_No_Ads": println("remove ads") removeAds() println("add coins to account") default: println("IAP not setup") } queue.finishTransaction(trans) break; case .Failed: println("buy error") queue.finishTransaction(trans) break; default: println("default") break; } }} // 6 func finishTransaction(trans:SKPaymentTransaction) { println("finish trans") SKPaymentQueue.defaultQueue().finishTransaction(trans) } //7 func paymentQueue(queue: SKPaymentQueue!,removedTransactions transactions: [AnyObject]!) { println("remove trans"); } @IBAction func btnRemoveAds(sender: UIbutton) { for product in List { println("ProdID") var prodID = product.productIDentifIEr println(prodID) if(prodID == "com.webstart.tomsgamesinc.Startlight_No_Ads") { p = product buyProduct() break; } }}
我确实在iTunes连接中添加了购买.
解决方法 我已经像这样更新了你的代码:import UIKitimport StoreKitclass VIEwController: UIVIEwController,SKProductsRequestDelegate,SKPaymentTransactionObserver { var productID = "com.webstart.tomsgamesinc.Startlight_No_Ads" var product: SKProduct? var isProductRecive = false overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() getProductInfo() SKPaymentQueue.defaultQueue().addTransactionObserver(self) } @IBAction func btnRemoveAds(sender: UIbutton) { if isProductRecive{ let payment = SKPayment(product: product) SKPaymentQueue.defaultQueue().addPayment(payment) }else{ let alert = UIAlertVIEw() alert.Title = "Alert" alert.message = "Retriving product please wait." alert.addbuttonWithTitle("Ok") alert.show() getProductInfo() } } func getProductInfo() { if SKPaymentQueue.canMakePayments() { let request = SKProductsRequest(productIDentifIErs: NSSet(objects: self.productID) as Set<NSObject>) request.delegate = self request.start() } else { let alert = UIAlertVIEw() alert.Title = "Alert" alert.message = "Please enable In App Purchase in Settings." alert.addbuttonWithTitle("Ok") alert.show() } } //3 func productsRequest(request: SKProductsRequest!,dIDReceiveResponse response: SKProductsResponse!) { var products = response.products if (products.count != 0) { product = products[0] as? SKProduct self.isProductRecive = true println(product) } else { let alert = UIAlertVIEw() alert.Title = "Alert" alert.message = "Product not found" alert.addbuttonWithTitle("Ok") alert.show() } products = response.invalIDProductIDentifIErs for product in products { println("Product not found: \(product)") } } // 4 func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) { println("transactions restored") var purchasedItemIDS = [] for transaction in queue.transactions { var t: SKPaymentTransaction = transaction as! SKPaymentTransaction let prodID = t.payment.productIDentifIEr as String switch prodID { case "com.webstart.tomsgamesinc.Startlight_No_Ads": println("remove ads") default: println("IAP not setup") } } } // 5 func paymentQueue(queue: SKPaymentQueue!,updatedTransactions transactions: [AnyObject]!) { for transaction in transactions as! [SKPaymentTransaction] { switch transaction.transactionState { case .Restored: SKPaymentQueue.defaultQueue().finishTransaction(transaction) case .Purchased: SKPaymentQueue.defaultQueue().finishTransaction(transaction) case .Failed: SKPaymentQueue.defaultQueue().finishTransaction(transaction) default: break } } } func request(request: SKRequest!,dIDFailWithError error: NSError!) { println("There was an error"); }}
它与我的应用程序包ID产品ID工作正常,所以请检查iTunes连接问题应该在那里.
总结以上是内存溢出为你收集整理的ios – productsRequest不适合快速进行应用程序内购买全部内容,希望文章能够帮你解决ios – productsRequest不适合快速进行应用程序内购买所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)