Swift3 苹果内购

Swift3 苹果内购

1、引入头文件

  import StoreKit

2、在viewDidLoad添加

  SKPaymentQueue.default().add(self) 
 
  //添加代理
  SKPaymentTransactionObserver,SKProductsRequestDelegate

2、支付

   if (SKPaymentQueue.canMakePayments()) {
        self.getProductInfo(payID: "苹果内购商品ID")
   }else{
        MDHubHelper.share.showError(msg: NSLocalizedString(str: "无法购买"))
   }

   func getProductInfo(payID:String) {
        SKPaymentQueue.default().add(self)
        let array = NSArray.init(object: payID)
        
        let request = SKProductsRequest.init(productIdentifiers: NSSet(array: array as! [Any]) as! Set<String>)
        request.delegate = self
        request.start()
    }
    
    func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {
        
    }
    
    func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
        
    }
    
    func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
        
    }
    
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case .purchased:
                self.completeTransaction(transaction: transaction)
            //请求服务端
            case .purchasing:
                MDHubHelper.share.showStatus(msg:NSLocalizedString(str: "正在支付"))
            //正在交易
            case .failed:
                MDHubHelper.share.showError(msg: NSLocalizedString(str: "支付失败"))
                self.failedTransaction(transaction: transaction)
            case .restored:
                MDHubHelper.share.showError(msg: NSLocalizedString(str: "已购买该商品"))
                self.restoreTransaction(transaction: transaction)
            default:
                self.failedTransaction(transaction: transaction)
                break
            }
        }
    }
    
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        
        let myProduct = response.products
        
        if (myProduct.count == 0) {
            MDHubHelper.share.showError(msg: NSLocalizedString(str: "无内购信息"))
            return
        }
        lPayment = SKPayment.init(product: myProduct[0])
        SKPaymentQueue.default().add(lPayment)
    }
    
    //自定义函数
    func completeTransaction(transaction:SKPaymentTransaction) {
        
        let recvURL = Bundle.main.appStoreReceiptURL
        let receiptData = NSData(contentsOf: recvURL!)
        let encodeStr = receiptData?.base64EncodedString(options: .endLineWithLineFeed)
        
        MDNetRequest.share.notifyApplePaySuccess(url: UrlApplePay, params: ["order_id":self.orderId!,"receipt":encodeStr!,"transactionId":transaction.transactionIdentifier!,"session_id":MDDataSource.share.session!], successBlock: {
            MDHubHelper.share.hidHubView()
            MDHubHelper.share.showError(msg: NSLocalizedString(str:"购买成功"))
        }, failBlock: {
            MDHubHelper.share.hidHubView()
            MDHubHelper.share.showError(msg: NSLocalizedString(str: "通知服务器失败"))
        }) { (err) in
            MDHubHelper.share.showError(msg: NSLocalizedString(str: "请求服务器错误"))
        }
        SKPaymentQueue.default().finishTransaction(transaction)
    }
    
    func failedTransaction(transaction:SKPaymentTransaction) {
        SKPaymentQueue.default().finishTransaction(transaction)
    }
    
    func restoreTransaction(transaction:SKPaymentTransaction) {
        SKPaymentQueue.default().finishTransaction(transaction)
    }

  • 发表于 2017-11-14 09:20
  • 阅读 ( 457 )
  • 分类:ios

相关问题

2 条评论

请先 登录 后评论
不写代码的码农
Alan

iOS开发

14 篇文章

作家榜 »

  1. 威猛的小站长 107 文章
  2. Jonny 65 文章
  3. 江南烟雨 33 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 20 文章
  8. Alan 14 文章