无法收到任何弹出窗口或提示用户登录的信息

时间:2019-01-08 23:48:44

标签: ios swift sandbox storekit

我遵循了一个演练,其中包括非消耗性的应用内购买。不幸的是,在用户单击购买按钮后,什么也没有发生。我没有收到任何登录iTunes或接受付款的提示。这里有我想念的东西吗?

我尝试了多次演练,它们似乎都具有相似的代码,我已经按照苹果网站的步骤进行了操作,但是我不确定是否必须先提交完整的应用程序,然后才能测试通过沙盒购买应用。

IAPService.swift

import Foundation
import StoreKit
import UIKit

class IAPService: NSObject {

    private override init() {}
    static let shared = IAPService()

    var products = [SKProduct]()
    let paymentQueue = SKPaymentQueue.default()

    func getProducts() {
        let products: Set = [IAPProduct.nonConsumable.rawValue]
       let request = SKProductsRequest(productIdentifiers: products)
        request.delegate = self
        request.start()
        paymentQueue.add(self)
    }

    func purchase(product: IAPProduct) {
        guard let productToPurchase = products.filter({ 
$0.productIdentifier == product.rawValue }).first else { return }
        let payment = SKPayment(product: productToPurchase)
        paymentQueue.add(payment)
    }

    func restorePurchases() {
        print("restore purchases")
        paymentQueue.restoreCompletedTransactions()
    }

}

extension IAPService: SKProductsRequestDelegate {
    func productsRequest(_ request: SKProductsRequest, didReceive 
         response: SKProductsResponse) {
            products = response.products
            for product in response.products {
            print(product.localizedTitle)
        }
    }
}

extension IAPService: SKPaymentTransactionObserver {
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions 
        transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
        print(transaction.transactionState.status(), 
        transaction.payment.productIdentifier)

            switch transaction.transactionState {
            case .purchasing: break
            default: queue.finishTransaction(transaction)
            }
        }
    }
}

extension SKPaymentTransactionState {
    func status() -> String {
        switch self {
        case .deferred: return "deferred"
        case .failed: return "failed"
        case .purchased: return "purchased"
        case .purchasing: return "purchasing"
        case .restored: return "restored"
        }
    }
}

在我的产品文件中

IAP.Products.swift

import Foundation

enum IAPProduct: String {
     case nonConsumable = "Quizly"
}

在我的mainVC中

override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        IAPService.shared.getProducts()
        print("IAP == \(IAPService.shared.products)") // Why is this an empty array?
    }

@objc func pressToGetPremium(_ sender : UIButton) {        
        IAPService.shared.purchase(product: .nonConsumable)
        print("IAP ===== \(IAPService.shared.products)")
}

当用户单击按钮时,我希望弹出一个窗口,使用户必须登录其Apple帐户,然后另一个弹出窗口将询问他们是否要接受非消耗性产品(1.99美元)等。但是我根本没有弹出窗口。

我收到了didReceive产品的回馈...

响应SKProducts响应0x00000002811cba10 baseNSObject @ 0 NSObject
isa类0x2811cba10 0x00000002811cba10 _internal SKProductsResponseInternal * 0x28139c0e0 0x000000028139c0e0 NSObject NSObject
_invalidIdentifiers __NSSingleObjectArrayI * 1个元素0x00000002811cb930 [0] __NSCFString *“ Quizly” 0x000000028139c620 NSMutableString NSMutableString _products __NSArray0 * 0元素0x00000002811c0050 NSArray NSArray NSObject NSObject
isa类__NSArray0 0x000001a25a605811

1 个答案:

答案 0 :(得分:1)

As per our comment discussion - The error produced from the didReceive products method indicates your bundleIdentifiers are not matching the ones on the appStore.