数组:response.products不包含我为应用程序创建的所有IAP产品,只有十二个中的四个

时间:2016-12-07 10:30:22

标签: ios objective-c xcode in-app-purchase

我已经访问了我的iTunes Connect帐户并创建了12个IAP产品。 五个耗材和七个非耗材。但是当我运行代码时它会向我显示错误,所以我想看看response.products数组中的内容:

NSLog(@"%@", response.products);

在控制台中,它会打印出一个只包含四个对象的数组。 以下是产品请求功能的代码:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse: (SKProductsResponse *)response{
int count = (int)[response.products count];
if(count > 0){

    if (productSelected) {

        _validProduct1 = [response.products objectAtIndex:0];

        [self purchase:_validProduct1];

    } else {

        NSLog(@"%@", response.products);

        _validProduct1 = [response.products objectAtIndex:0];
        _validProduct2 = [response.products objectAtIndex:1];
        _validProduct3 = [response.products objectAtIndex:3];
        _validProduct4 = [response.products objectAtIndex:4];
        _validProduct5 = [response.products objectAtIndex:5];
        _validProduct6 = [response.products objectAtIndex:6];

        _validProduct7 = [response.products objectAtIndex:7];
        _validProduct8 = [response.products objectAtIndex:8];
        _validProduct9 = [response.products objectAtIndex:9];
        _validProduct10 = [response.products objectAtIndex:10];
        _validProduct11 = [response.products objectAtIndex:11];
        _validProduct12 = [response.products objectAtIndex:12];

对于我遇到的问题,其余功能不是必需的。 我检查了每个产品ID,但我的代码中它们完全相同。

1 个答案:

答案 0 :(得分:0)

您是否传递了在iTunes Connect中注册的正确产品ID?我通常有一个包含所有产品ID的.plist文件。

您可以像这样请求您的产品:

public typealias ProductsRequestCompletionHandler = (_ success: Bool, _ products: [SKProduct]?) -> Void
public typealias PaymentCompletionHandler = (_ productIdentifier: String?, _ errorMessage: String?) -> Void
public typealias RestoreCompletionHandler = (_ errorMessage: String?) -> Void

class InAppPurchaseHelper: NSObject {

    static let shared = InAppPurchaseHelper()
    fileprivate var productsRequest: SKProductsRequest?
    fileprivate let bundleIdentifier = Bundle.main.bundleIdentifier ?? ""

    var productsRequestCompletionHandler: ProductsRequestCompletionHandler?
    var paymentCompletionHandler: PaymentCompletionHandler?
    var restoreCompletionHandler: RestoreCompletionHandler?

    fileprivate lazy var localProductList: [String] = {
        // We don't need the bundle identifier for in-app purchase products.
       // This is only for checking if you have a test env, because you need different products for Beta testers. (with different IDs)
        let productPrefix = self.bundleIdentifier.contains("test") ? "test_" : ""
        let productIdsURL = Bundle.main.url(forResource: "products", withExtension: "plist")!
        let productIds = NSArray(contentsOf: productIdsURL) as! [String]
        return productIds.map({ "\(productPrefix)\($0)" })
    }()

    fileprivate lazy var productIds: Set<String> = {
        return Set<String>(self.localProductList)
    }()

    private override init () {
        super.init()
        SKPaymentQueue.default().add(self)
    }

    /**
     Request our valid and invalid products listed in the iTunes Connect

     - Parameter completion: fetched products upon successful completion
     */
    func requestProducts(completion: ProductsRequestCompletionHandler?) {
        self.productsRequest?.cancel()
        self.productsRequestCompletionHandler = completion
        let productsRequest = SKProductsRequest(productIdentifiers: self.productIds)
        productsRequest.delegate = self
        self.productsRequest = productsRequest
        productsRequest.start()
    }
}

您可以检查有效和无效的产品:

extension InAppPurchaseHelper: SKProductsRequestDelegate {
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

        // TODO: - Handle invalid product IDs
        let invalidProductIds = response.invalidProductIdentifiers
        print("Invalid Products: \(invalidProductIds)")

        for p in response.products {
            print("Product: \(p.productIdentifier) - \(p.localizedTitle) - \(p.localizedDescription)")
        }
        self.productsRequestCompletionHandler?(true, response.products)

    }
    func request(_ request: SKRequest, didFailWithError error: Error) {
        print("SKRequest did fail with Error")
        self.productsRequestCompletionHandler?(false, nil)
    }

}