gpt4 book ai didi

view - 我的 IAP 不工作。 func Paymentqueue 的错误

转载 作者:行者123 更新时间:2023-12-02 08:24:11 26 4
gpt4 key购买 nike

当我点击我的任何一个 IAPS 时,会出现一条消息说 IAP 已经购买(事实并非如此)并将免费恢复,然后没有任何反应,IAP 不会执行。

这是我在控制台中得到的:

No Value.
IAP is enabled, loading...

true
Product Request
Product Added
IAP id
Remove Ads
Removes ads from the app.
2
Buy: IAP id
Add Payment
nil
default: Error

这是我的代码:

GameViewController.Swift

import UIKit
import StoreKit


class GameViewController: UIViewController, ADBannerViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver, GKGameCenterControllerDelegate,GADBannerViewDelegate{


@IBOutlet var outRemoveAds: UIButton!
@IBOutlet var outRestorePurchases: UIButton!


override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

if NSUserDefaults.standardUserDefaults().objectForKey("val") != nil {
print("Has a value.")
banner.removeFromSuperview()
bannerGoogle.removeFromSuperview()
outRemoveAds.removeFromSuperview()
outRestorePurchases.removeFromSuperview()
removeInterFrom = 1
}
else {
print("No Value.")
}

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

}

}


//IAP Ads

@IBAction func removeAds(sender: UIButton) {
for product in list{
let prodID = product.productIdentifier
if (prodID == "IAP id"){
p = product
buyProduct()
break
}
}
}
@IBAction func restorePurchases(sender: UIButton) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}


//IAP Functions

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

func removeAds(){
banner.removeFromSuperview()
bannerGoogle.removeFromSuperview()
outRemoveAds.removeFromSuperview()
outRestorePurchases.removeFromSuperview()
let theValue = 10
NSUserDefaults.standardUserDefaults().setObject(theValue, forKey: "val")
NSUserDefaults.standardUserDefaults().synchronize()
}

func buyProduct(){
print("Buy: "+p.productIdentifier)
let pay = SKPayment (product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}

func productsRequest(request: SKProductsRequest, didReceiveResponse 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 as SKProduct)
}
}

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("IAP unlocked")
print(p.productIdentifier)

let prodID = p.productIdentifier as String
switch prodID{
case "IAP id":
print("Remove Ads")
removeAds()
default:
print("IAP not setup")
}
queue.finishTransaction(trans)
break
case .Failed:
print ("Buy error")
queue.finishTransaction(trans)
break
default:
print("default: Error")
break
}
}
}

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("Purchases Restored")

_ = []
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction as SKPaymentTransaction

let prodID = t.payment.productIdentifier as String
switch prodID{
case "IAP id":
print("Remove Ads")
removeAds()
default:
print("IAP not setup")
}


}
}
func finishTransaction(trans:SKPaymentTransaction){
print("Finshed Transaction")
}

func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("Remove Transaction")
}
}

MenuViewController.swift

import UIKit
import StoreKit
import Social




class MenuViewController: UIViewController, SKProductsRequestDelegate,SKPaymentTransactionObserver {

@IBOutlet var outRestart: UIButton!
@IBOutlet var outBuy: UIButton!



override func viewDidLoad() {
super.viewDidLoad()

}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)


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

}

}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func buy(sender: UIButton) {
for product in list{
let prodID = product.productIdentifier
if (prodID == "IAP id"){
p = product
buyProduct()
break
}
}

}

//IAP Functions

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

func keepOn(){
buyornot = 1
Back()
}

func buyProduct(){
print("Buy: "+p.productIdentifier)
let pay = SKPayment (product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}

func productsRequest(request: SKProductsRequest, didReceiveResponse 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 as SKProduct)
}
}

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("IAP unlocked")
print(p.productIdentifier)

let prodID = p.productIdentifier as String
switch prodID{
case "IAP id":
print("Keep on")
keepOn()
default:
print("IAP not setup")
}
queue.finishTransaction(trans)
break
case .Failed:
print ("Buy error")
queue.finishTransaction(trans)
break
default:
print("default: Error")
break
}
}
}

func finishTransaction(trans:SKPaymentTransaction){
print("Finshed Transaction")
}

func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("Remove Transaction")
}

最佳答案

我找到了解决方案!

删除

SKPaymentQueue.defaultQueue().addTransactionObserver(self) 

在任何你有它的地方,把它放在一个地方,每次你的应用程序启动时它都会被执行(我把它放在 viewDidLoad() 中)。

这将检查所有未完成的交易并在应用加载后终止它们,从而在您的用户触发 IAP 之前消除任何可能的错误。

(如果这个答案,或者这个问题对你有帮助,别忘了点赞 ;))

P.S.:此外,这不是我的问题,但请确保为每个 PurchaseState 执行 finishTransaction(),如下所示:

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("IAP unlocked")
print(p.productIdentifier)

let prodID = p.productIdentifier as String
switch prodID{
case "IAP id":
print("Keep on")
keepOn()
default:
print("IAP not setup")
}
queue.finishTransaction(trans)
break
case .Failed:
print ("Buy error")
queue.finishTransaction(trans)
break
default:
print("default: Error")
break
}
}
}

永远不要忘记这一点:

 queue.finishTransaction(trans)  

关于view - 我的 IAP 不工作。 func Paymentqueue 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33741619/

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