IAP - Swift 3.0

时间:2017-02-16 11:15:24

标签: swift in-app-purchase xcode8.2 skproduct

我想为我的Swift应用程序做IAP,每当我尝试通过代码连接intunes时,我的xcode中缺少权利,请参阅附件。

这是我连接itunes connect的代码。

func loadStoreProducts(){


    if (SKPaymentQueue.canMakePayments()){
        KVNProgress.show(withStatus: "Loading payment options")
        let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: Set([productID]));
        productsRequest.delegate = self;
        productsRequest.start();
    }else{
        AppDelegate.getAppDelegate().showMessage("Your Apple account doesn't allow payments, you cannot have a JustAskMe subscription.")
    }
}

func restoreTransactions(){
    SKPaymentQueue.default().restoreCompletedTransactions()
}

func buyMembership(product:SKProduct){
    let payment = SKPayment(product: product)
    SKPaymentQueue.default().add(payment);
}

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

    DispatchQueue.main.async(){
        KVNProgress.dismiss()
    }


    if (!response.products.isEmpty) {

        let  validProduct: SKProduct = response.products[0] as SKProduct

        if (validProduct.productIdentifier == self.productID) {
            print(validProduct.localizedTitle)
            print(validProduct.localizedDescription)
            print(validProduct.price)
            buyMembership(product: validProduct);
        } else {
            // got a product that wasn't the one we asked for?
            print("Invalid product found: \(validProduct.productIdentifier)")
        }
    } else {
        AppDelegate.getAppDelegate().showMessage("Problems processing your payment, please try again.")
    }
}

func request(_ request: SKRequest, didFailWithError error: Error) {

    DispatchQueue.main.async(){
        KVNProgress.dismiss()
    }
    // notify the user
    AppDelegate.getAppDelegate().showMessage("Problems processing your payment, please try again.")
    //log the issue.
    print(error.localizedDescription)

}


func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
    DispatchQueue.main.async(){
        KVNProgress.dismiss()
    }

    for transaction:AnyObject in queue.transactions
    {
        let trans : SKPaymentTransaction = transaction as! SKPaymentTransaction
        let identifier : NSString = trans.payment.productIdentifier as NSString
        print(identifier)
    }

}

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    DispatchQueue.main.async(){
        KVNProgress.dismiss()
    }

}

func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
    DispatchQueue.main.async(){
        KVNProgress.dismiss()
    }
    // notify the user
    AppDelegate.getAppDelegate().showMessage("Problems loading your previous purchases, please try again.")
    //log the issue.
    print(error.localizedDescription)
}

enter image description here

2 个答案:

答案 0 :(得分:0)

您是否在Xcode的Capabilities Pane开启了应用内购买?

答案 1 :(得分:0)

登录开发人员中心,然后导航到证书,标识符&配置文件,应用程序ID和检查应用程序内购买是否已启用,然后重试。