swift 文章收集Swift开发iOS-开发

iOS Swift 开发内购实现 SwiftyStoreKit

2018-01-09  本文已影响681人  GloryMan

title: Xcode 8.3.2 Swift 实现内购
date: 2017-05-25 14:58:32
categories:

tags:
- SwiftyStoreKit
- IAP


iOS 开发内购实现

代码环境

需要的轮子

开始使用 (ItunesConnect)

Github Demo 地址

你需要有一个 App ( 肯定要有 付费的开发者账号 )

使用此 App 的bundleID 唯一标示

Cocoapods 导入 SwiftyStoreKit

一切准备就绪-下面代码部分

import SwiftyStoreKit


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
    
        for purchase in purchases {
    
            if purchase.transaction.transactionState == .purchased || purchase.transaction.transactionState == .restored {
    
               if purchase.needsFinishTransaction {
                   // Deliver content from server, then:
                   SwiftyStoreKit.finishTransaction(purchase.transaction)
               }
               print("purchased: \(purchase)")
            }
        }
    }
    return true
}
func getList() {
        SwiftyStoreKit.retrieveProductsInfo(["图1 内购项目的 产品ID 这个一般存储在服务器里"]) { result in
            if let product = result.retrievedProducts.first {
                let priceString = product.localizedPrice!
                print("Product: \(product.localizedDescription), price: \(priceString)")
            } else if let invalidProductId = result.invalidProductIDs.first {
                print("Invalid product identifier: \(invalidProductId)")
            } else {
                print("Error: \(result.error)")
            }
        }
    }

SwiftyStoreKit.purchaseProduct("产品ID", quantity: 1, atomically: true) { result in
    switch result {
    case .success(let purchase):
        print("Purchase Success: \(purchase.productId)")
    case .error(let error):
        switch error.code {
        case .unknown: print("Unknown error. Please contact support")
        case .clientInvalid: print("Not allowed to make the payment")
        case .paymentCancelled: break
        case .paymentInvalid: print("The purchase identifier was invalid")
        case .paymentNotAllowed: print("The device is not allowed to make the payment")
        case .storeProductNotAvailable: print("The product is not available in the current storefront")
        case .cloudServicePermissionDenied: print("Access to cloud service information is not allowed")
        case .cloudServiceNetworkConnectionFailed: print("Could not connect to the network")
        case .cloudServiceRevoked: print("User has revoked permission to use this cloud service")
        }
    }
}
    // 本地验证(SwiftyStoreKit 已经写好的类) AppleReceiptValidator
    // .production 苹果验证  .sandbox 本地验证
 let receipt = AppleReceiptValidator(service: .production)
 let password = "公共秘钥"
 SwiftyStoreKit.verifyReceipt(using: receipt, password: password, completion: { (result) in
     switch result {
     case .success(let receipt):
      print("receipt--->\(receipt)")
         break
     case .error(let error):
      print("error--->\(error)")
         break
     }
 })

完成了,是不是很简单,是不是很好理解。

基本阅读 --> SwiftyStoreKit

延伸阅读

App 内购验证

[官方文档]
(https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html)

License

AppPurchasesDemo is released under the MIT license. See LICENSE for details.

上一篇下一篇

猜你喜欢

热点阅读