我写了一些代码来让我的应用内购买摆脱我的广告。响应返回空白,并返回错误,因为它是零,这是我的应用程序内购买的完整代码:
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)
}
//3
func 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
}
// 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")
removeAds()
default:
println("IAP not setup")
}
}
}
// 5
func 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连接中添加了购买。
答案 0 :(得分:1)
我已经像这样更新了你的代码:
import UIKit
import StoreKit
class 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连接问题应该在那里。
答案 1 :(得分:0)
此代码适用于我的游戏,并返回有多少产品可供购买。适用于xCode 7.1.1。
var products = [SKProduct]()
var product: SKProduct?
//StoreKit protocol method. Called when the AppStore responds
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
var item = response.products
if item.count != 0 {
for var i = 0; i < item.count; i++ {
product = item[i] as SKProduct
products.append(product!)
}
}
print("There are \(products.count) items to buy.")
}