Xcode 12 in app 购买无法在设备上运行

时间:2021-01-26 15:28:06

标签: ios xcode in-app-purchase storekit

应用内购买正在模拟器上运行,但不适用于真实设备,我使用的是 XCode 12 并在我的方案中添加了 StoreKit 配置文件。

在模拟器上一切正常,但在真实设备上却没有。 请帮忙!!这是我的代码。

这些只是为了避免 stackoverflow 的愚蠢验证。它说我需要写的不仅仅是我的代码行。所以我只是多写几行

import StoreKit
import UIKit

class PurchaseViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver, UITextFieldDelegate {
    
    var myProduct: SKProduct?

    @IBOutlet weak var warnlabel: UILabel!
    @IBOutlet weak var subemail: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        SKPaymentQueue.default().add(self)
        // Do any additional setup after loading the view.
        fetchProducts()
        
    }
    
    enum Product: String, CaseIterable {
        case subscription = "identifier"
    }
    
    private func fetchProducts() {
        let request = SKProductsRequest(productIdentifiers: Set(Product.allCases.compactMap({$0.rawValue})));
        request.delegate = self
        request.start()
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return(true)
    }
    @IBAction func backhome(_ sender: Any) {
        let homeVC = self.storyboard?.instantiateViewController(identifier: "HomeVC") as! HomeViewController
        self.navigationController?.pushViewController(homeVC, animated: true)
    }
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        if let product = response.products.first{
            myProduct = product
            DispatchQueue.main.async {
                self.warnlabel.text = product.productIdentifier
            }
            print(product.price)
        }
        
    }
    
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions{
            switch transaction.transactionState {
            case .purchasing:
                // no one here
                break
            case .purchased, .restored:
                //do subscription
                SKPaymentQueue.default().finishTransaction(transaction)
                SKPaymentQueue.default().remove(self)
                break
            case .failed, .deferred:
                SKPaymentQueue.default().finishTransaction(transaction)
                SKPaymentQueue.default().remove(self)
                break
            default:
                SKPaymentQueue.default().finishTransaction(transaction)
                SKPaymentQueue.default().remove(self)
                break
            }
        }
    }

    @IBAction func subbutt(_ sender: Any) {
        guard let myProduct = myProduct else{
            return
        }
        guard let email = subemail.text, email.isEmpty else{
            let payment = SKPayment(product: myProduct)
            SKPaymentQueue.default().add(payment)
            return
        }
        DispatchQueue.main.async {
            self.warnlabel.text = "please enter an email"
        }
        
    }
    

}

0 个答案:

没有答案