TheRapture87
TheRapture87

Reputation: 1423

In-App Purchase: list of product identifiers is always empty

I am trying to create a store page for my app so the user can buy coins or whatever but when i try to get the item list it doesn't seem to display. It prints "Product Request" but it doesn't print the item. The items appear in the itunes connect page and I've used the same ID so I'm pretty sure something is wrong with my code.

   class storeController : UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

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



        func buyProduct(){
            print("buy \(p.productIdentifier)")
            var pay = SKPayment(product: p)
            SKPaymentQueue.defaultQueue().addTransactionObserver(self)
            SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
        }

        override func viewDidLoad() {

            if(SKPaymentQueue.canMakePayments()) {
                print("IAP is enabled, loading...")
                var productID : NSSet = NSSet(object: "myapp.iap.500coins")
                var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
                request.delegate = self
                request.start()
            } else {
                print("please enable IAPs")
            }

        }

        func requestDidFinish(request: SKRequest) {

        }

        func request(request: SKRequest, didFailWithError error: NSError) {

        }

        func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])    {
            print("add payment")

            for transactions:AnyObject in transactions {
                var trans = transactions as! SKPaymentTransaction
                print(trans.error)

                switch trans.transactionState {

                case .Purchased:
                    print("unlock iap here")
                    print(p.productIdentifier)

                    let prodID = p.productIdentifier as String
                    switch prodID {
                    case "myapp.iap.500coins" :
                        print("give coins")
                    default:
                        print("didnt go through")
                    }
                    queue.finishTransaction(trans)
                    break;
                case .Failed:
                    print("error")
                    queue.finishTransaction(trans)
                    break;
                default:
                    print("default")
                }
            }
            }

        func finishTransaction(trans:SKPaymentTransaction) {
            print("finished trans")
        }

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

        func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
            print("Product Request")
            let myProduct = response.products

            for product in myProduct {
                print("product found")
                print(product.productIdentifier)
                print(product.localizedTitle)
                print(product.localizedDescription)
                list.append(product)
            }
        }
    }

Upvotes: 1

Views: 949

Answers (1)

joern
joern

Reputation: 27620

Your code looks fine. However I suspect that there is something wrong with your product identifiers.

SKProductsResponse contains an array of product identifiers that were not recognized by the Apple App Store. When everything went ok this list is empty. Check that list to see if there were problems with your products:

func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {                   
    if !response.invalidProductIdentifiers.isEmpty {
        print("invalid:" + response.invalidProductIdentifiers)
    }

    print("Product Request")
    let myProduct = response.products
    ...
}

This will not fix your problem but at least you will know why there are no products in your response.

Upvotes: 4

Related Questions