gpt4 book ai didi

Swift 应用内购买不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 06:04:11 25 4
gpt4 key购买 nike

我正在制作 iPhone 游戏。我正在关注用于游戏资金结算的 Swift In-App Billing 教程。我完成了所有 Swift 编码和构建,我点击了屏幕上的按钮,但弹出窗口没有弹出。请让我知道我的代码有什么问题。

 import UIKit
import AVFoundation
import StoreKit

class ViewController: UIViewController,UIScrollViewDelegate,SKProductsRequestDelegate, SKPaymentTransactionObserver {



@IBOutlet var outAddCoins: UIButton!


@IBAction func btnAddCoins(_ sender: Any) {
for product in list {
let prodID = product.productIdentifier
if(prodID == "Add500000Credit") {
p = product
buyProduct()
}
}
}


func buyProduct() {
print("buy " + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().add(pay as SKPayment)
}


func addCoins() {
playerMoney += 500000
MoneyLabel.text = "\(playerMoney)"
}

var list = [SKProduct]()
var p = SKProduct()






func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("product request")
let myProduct = response.products
for product in myProduct {
print("product added")
print(product.productIdentifier)
print(product.localizedTitle)
print(product.localizedDescription)
print(product.price)

list.append(product)
}

outAddCoins.isEnabled = true
}




func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("transactions restored")
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String

switch prodID {

case "Add500000Credit":
print("add coins to account")
addCoins()
default:
print("IAP not found")
}
}
}






func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add payment")

for transaction: AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
print(trans.error)

switch trans.transactionState {
case .purchased:
print("buy ok, unlock IAP HERE")
print(p.productIdentifier)

let prodID = p.productIdentifier
switch prodID {

case "Add500000Credit":
print("add coins to account")
addCoins()
default:
print("IAP not found")
}
queue.finishTransaction(trans)
case .failed:
print("buy error")
queue.finishTransaction(trans)
break
default:
print("Default")
break
}
}
}

这是viewdidload部分

override func viewDidLoad() {
super.viewDidLoad()



outAddCoins.isEnabled = false







if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")
let productID: NSSet = NSSet(objects: "Add500000Credit")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
}

我正在使用最新版本的 Xcode。我已经在 iTunes Connect 中完成了应用内购买的所有配置,但是构建后没有弹出购买弹窗。非常感谢您的帮助。

buy Add500000Credit
add payment
nil
Default
add payment
Optional(Error Domain=SKErrorDomain Code=0 "can not connect to the iTunes Store" UserInfo={NSLocalizedDescription=can not connect to the iTunes Store})
buy error

最佳答案

测试应用内购买很棘手:

  • 确保您已创建用于测试的沙盒帐户。

  • 在设备上进行测试,而不是在模拟器中。您无法在模拟器中进行测试。

  • 并且记得在测试前退出设备上的 iCloud。否则您将无法使用沙盒帐户。

关于Swift 应用内购买不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41722492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com