调用In App Purchase时出错 - swift

时间:2015-10-15 03:59:01

标签: ios swift in-app-purchase

我在调用以下功能时遇到问题(或购买产品):

  

func restoreCompletedTransactions(){           SKPaymentQueue.defaultQueue()restoreCompletedTransactions();       }

它返回如下错误:

  

无法连接到iTunes Store“   UserInfo = {NSLocalizedDescription =无法连接到iTunes Store,   SSErrorHTTPStatusCodeKey = 503}

错误是什么? 我怎么解决呢?

由于

1 个答案:

答案 0 :(得分:0)

之前我遇到过这个问题!它非常容易修复。确保您使用常规帐户和iTunes退出应用商店。

下次在设备上测试IAP时,请使用您的沙盒帐户登录。它应该工作。

此外,如果这不能解决您的问题,请看一下:My IAP isn't working. Bugs at func Paymentqueue

它包含我的IAP代码(可行)以及修复某些问题的一些提示。我希望这个答案很有帮助;)

以下是完整的IAP代码:

import UIKit
import StoreKit


class GameViewController: UIViewController, ADBannerViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver, GKGameCenterControllerDelegate,GADBannerViewDelegate{


    @IBOutlet var outRemoveAds: UIButton!
    @IBOutlet var outRestorePurchases: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if NSUserDefaults.standardUserDefaults().objectForKey("val") != nil {
            print("Has a value.")
            banner.removeFromSuperview()
            bannerGoogle.removeFromSuperview()
            outRemoveAds.removeFromSuperview()
            outRestorePurchases.removeFromSuperview()
            removeInterFrom = 1
        }
        else {
            print("No Value.")
        }

        if(SKPaymentQueue.canMakePayments()){
            print("IAP is enabled, loading...")
            let productID:NSSet = NSSet(objects:"IAP id")
            let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
            request.delegate = self
            request.start()
        }
        else{
            print("Please enable IAPS")

        }

    }


    //IAP Ads

    @IBAction func removeAds(sender: UIButton) {
        for product in list{
            let prodID = product.productIdentifier
            if (prodID == "IAP id"){
                p = product
                buyProduct()
                break
            }
        }
    }
    @IBAction func restorePurchases(sender: UIButton) {
        SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
    }


    //IAP Functions

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

    func removeAds(){
        banner.removeFromSuperview()
        bannerGoogle.removeFromSuperview()
        outRemoveAds.removeFromSuperview()
        outRestorePurchases.removeFromSuperview()
        let theValue = 10
        NSUserDefaults.standardUserDefaults().setObject(theValue, forKey: "val")
        NSUserDefaults.standardUserDefaults().synchronize()
    }

    func buyProduct(){
        print("Buy: "+p.productIdentifier)
        let pay = SKPayment (product: p)
        SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
    }

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

        for product in myProduct{
            print("Product Added")
            print(product.productIdentifier)
            print(product.localizedTitle)
            print(product.localizedDescription)
            print(product.price)

            list.append(product as SKProduct)
        }
    }

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

        for transaction:AnyObject in transactions{
            let trans = transaction as! SKPaymentTransaction
            print(trans.error)
            switch trans.transactionState{
            case .Purchased:
                print("IAP unlocked")
                print(p.productIdentifier)

                let prodID = p.productIdentifier as String
                switch prodID{
                case "IAP id":
                    print("Remove Ads")
                    removeAds()
                default:
                    print("IAP not setup")
                }
                queue.finishTransaction(trans)
                break
            case .Failed:
                print ("Buy error")
                queue.finishTransaction(trans)
                break
            default:
                print("default: Error")
                break
            }
        }
    }

    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
        print("Purchases Restored")

        _ = []
        for transaction in queue.transactions {
            let t: SKPaymentTransaction = transaction as SKPaymentTransaction

            let prodID = t.payment.productIdentifier as String
            switch prodID{
            case "IAP id":
                print("Remove Ads")
                removeAds()
            default:
                print("IAP not setup")
            }


        }
    }
    func finishTransaction(trans:SKPaymentTransaction){
        print("Finshed Transaction")
    }

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

放:

SKPaymentQueue.defaultQueue().addTransactionObserver(self) 

在viewDidLoad或viewDidAppear