很抱歉我想要的所有代码都是正确的。我有三种不同的应用内购买,这是我在GameViewController中的代码。我在viewDidLoad()函数中调用了这些函数。有没有人看到代码有什么问题?
编辑:
override func viewDidLoad() {
super.viewDidLoad()
callthis()
callthis2()
callthis3()
}
func callthis() {
if(SKPaymentQueue.canMakePayments()) {
println("IAP is enabled, loading")
var productID:NSSet = NSSet(object: "unlockLevelTwo")
var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
request.delegate = self
request.start()
} else {
println("please enable IAPS")
}
}
func callthis2() {
if(SKPaymentQueue.canMakePayments()) {
println("IAP is enabled, loading")
var productID:NSSet = NSSet(object: "unlockLevelThree")
var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
request.delegate = self
request.start()
} else {
println("please enable IAPS")
}
}
func callthis3() {
if(SKPaymentQueue.canMakePayments()) {
println("IAP is enabled, loading")
var productID:NSSet = NSSet(object: "unlockEverything")
var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
request.delegate = self
request.start()
} else {
println("please enable IAPS")
}
}
func RestorePurchases() {
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
println("Can't make purchases")
}
var list = [SKProduct]()
var p = SKProduct()
func buyProduct() {
println("buy" + p.productIdentifier)
var pay = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}
func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
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)
}
}
func request(request: SKRequest!, didFailWithError error: NSError!) {
println("Error fetching product info")
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
println("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
var trans = transaction as! SKPaymentTransaction
println(trans.error)
switch trans.transactionState {
case .Purchased, .Restored:
let prodID = trans.payment.productIdentifier as String
switch prodID {
case "unlockLevelTwo":
println("unlocksleveltwo")
NSUserDefaults().setBool(true, forKey: "Leavel2")
gameScene.unlockLevelTwoPlease()
case "unlockLevelThree":
println("unlocklevelthree")
NSUserDefaults().setBool(true, forKey: "Leavel3")
gameScene.unlockLevelThreePlease()
case "unlockEverything":
println("unlockeverything")
NSUserDefaults().setBool(true, forKey: "Leavel2")
gameScene.unlockLevelTwoPlease()
NSUserDefaults().setBool(true, forKey: "Leavel3")
gameScene.unlockLevelThreePlease()
defaults.setBool(true , forKey: "removeAdsPurchased")
default:
println("unlocksEverything")
}
queue.finishTransaction(trans)
break;
case .Failed:
println("Purchased Failed");
queue.finishTransaction(trans)
break;
default:
println("default")
break;
}
}
}
func finishTransaction(trans:SKPaymentTransaction) {
println("finish trans")
SKPaymentQueue.defaultQueue().finishTransaction(trans)
}
func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!) {
println("remove trans");
}
然后在我的GameScene.swift文件中执行此操作:
if node.name == "unlockleveltwobutton" {
for product in viewController.list {
var prodID = product.productIdentifier
if(prodID == "unlockLevelTwo") {
viewController.p = product
viewController.buyProduct()
break;
}
}
runAction(menuAudioPlayer)
}
if node.name == "unlocklevelthreebutton" {
for product in viewController.list {
var prodID = product.productIdentifier
if(prodID == "unlockLevelThree") {
viewController.p = product
viewController.buyProduct()
break;
}
}
runAction(menuAudioPlayer)
}
if node.name == "specialdeal" {
for product in viewController.list {
var prodID = product.productIdentifier
if(prodID == "unlockEverything") {
viewController.p = product
viewController.buyProduct()
break;
}
}
runAction(menuAudioPlayer)
}