gpt4 book ai didi

ios - 应用内购买和恢复按钮 : Single Product - Non-Consumable

转载 作者:行者123 更新时间:2023-11-28 06:49:29 31 4
gpt4 key购买 nike

这个问题困扰了我好几天。

我有一个简单的应用程序,可以显示横幅和插页式广告。

我正在使用单个 View 应用程序,有主视图 Controller (ViewController.swift) 并设置了另一个 View Controller (InAppViewController.swift) 来处理弹出-向上页面:

  1. 允许用户进行应用内购买以移除所有广告(广告横幅和插页式广告);或
  2. 恢复购买。

我的代码在运行时没有错误。

应用内购买运行正常,但偶尔我会收到两次登录 iTunes 的请求。

但我的恢复按钮和相关功能似乎是问题所在。

我设置了多个沙盒测试员帐户进行测试,未购买该应用的新用户能够成功恢复购买。这应该是不可能的,所以我肯定在这里做错了什么。

这是我的代码:

主视图 Controller :

//  ViewController.swift

import UIKit
import MessageUI
import Social
import iAd
import StoreKit

class ViewController: UIViewController, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate, ADBannerViewDelegate, ADInterstitialAdDelegate
{

let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;

override func viewDidLoad() {
product_id = "some.product.id";
super.viewDidLoad()

//Check if product is purchased
if (defaults.boolForKey("purchased")){
print("already purchased")

// Hide or show banner ads is purchased/not purchased.
// Advertising Banner:
self.canDisplayBannerAds = false
}

else if (!defaults.boolForKey("stonerPurchased")){
print("not yet purchased")

// Advertising Banner:
self.canDisplayBannerAds = true
}

这段代码似乎工作得很好。当应用加载时,它能够确定谁支付了移除广告的费用以及那些没有支付的费用,并且广告横幅会适当显示。

我在第二个 View Controller (InAppPViewController.swift) 中遇到了问题。

这是我的代码:

第二个 View Controller - InAppViewController.swift:

//  InAppPViewController.swift

import UIKit
import StoreKit
import iAd

class InAppPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;


@IBOutlet weak var unlockAction: UIButton!
@IBOutlet var adBannerView: ADBannerView?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

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

@IBAction func restorePurchases(sender: UIButton) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()

}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {

print("Transactions Restored")
let alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}


@IBAction func unlockAction(sender: AnyObject) {

product_id = "some.product.id";

SKPaymentQueue.defaultQueue().addTransactionObserver(self)

//Check if product is purchased

if (defaults.boolForKey("purchased")){

}
else if (!defaults.boolForKey("stonerPurchased")){
print("false")
}


print("About to fetch the products");
// We check that we are allowed to make the purchase.

if (SKPaymentQueue.canMakePayments())
{
let productID:NSSet = NSSet(object: self.product_id!);
let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
productsRequest.delegate = self;
productsRequest.start();
print("Fething Products");
}else{
print("can't make purchases");
}

}

func buyProduct(product: SKProduct){
print("Sending the Payment Request to Apple");
let payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);

}
//Delegate Methods for IAP

func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {

let count : Int = response.products.count
if (count>0) {

let validProduct: SKProduct = response.products[0] as SKProduct
if (validProduct.productIdentifier == self.product_id) {
print(validProduct.localizedTitle)
print(validProduct.localizedDescription)
print(validProduct.price)
buyProduct(validProduct);
} else {
print(validProduct.productIdentifier)
}
} else {
print("nothing")
}
}


func request(request: SKRequest, didFailWithError error: NSError) {
print("Error Fetching product information");
}

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("Received Payment Transaction Response from Apple");

for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {

case .Purchased:
print("Product Purchased");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
defaults.setBool(true , forKey: "purchased")
break;

case .Failed:
print("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break;

case .Restored:
print("Already Purchased");
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()


default:
break;
}
}
}

}

我哪里错了?

问题:

  1. 我上面的代码是否正确?
  2. 我应该修改什么,为什么?

提前致歉,我是这个美妙的编码世界的新手……但我热爱其中的每一分钟!

最佳答案

  1. SKPaymentQueue.defaultQueue().addTransactionObserver(self) 应该在 viewDidLoad() 中而不是在 restore func 中
  2. 你可以像这样放置 SKPaymentQueue.defaultQueue().restoreCompletedTransactions()override func viewWillDisappear(animated: Bool) {
    SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
    }
  3. 在沙箱中登录两次是正常的。

希望我有所帮助。

关于ios - 应用内购买和恢复按钮 : Single Product - Non-Consumable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35315949/

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