StoreKit-2

import UIKit

import StoreKit

 

class ViewController: UITableViewController {

    

    // 数据源, 用于显示可以被销售的商品列表

    var products: [SKProduct] = [SKProduct]() {

        didSet {

            tableView.reloadData()

        }

    }

    

    deinit {

        SKPaymentQueue.defaultQueue().removeTransactionObserver(self)

    }

    

    

    @IBAction func retore(sender: AnyObject) {

        

        SKPaymentQueue.defaultQueue().restoreCompletedTransactions()

        SKPaymentQueue.defaultQueue().addTransactionObserver(self)

        

    }

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        // 1. 从我们自己的服务器, 获取需要销售的额商品

        LZJDataTool.getGoodsList { (ids: Set<String>) -> () in

            

            // 2. 拿到需要销售的额商品, 到苹果服务器, 进行验证, 看下, 哪些商品, 才可以真正被销售

            // 2.1 创建一个商品请求, 请求哪些商品可以真正的呗销售

            let request: SKProductsRequest = SKProductsRequest(productIdentifiers: ids)

            

            // 2.1.1 设置代理, 接收可以被销售的商品列表数据

            request.delegate = self

            

            // 2.2 发送请求

            request.start()

            

        }

        

    }

    

    

}

 

// tableview数据源方法/代理方法

extension ViewController {

    

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return products.count

    }

    

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        

        let cell = tableView.dequeueReusableCellWithIdentifier("product")

        

        

        // 获取对应的商品数据模型, 进行赋值

        let product = products[indexPath.row]

        

        // SKProduct 系统提供的商品模型

        // product.localizedTitle 商品名称

        // product.localizedDescription 商品描述

        // product.price 商品价格

        cell?.textLabel?.text = product.localizedTitle

        cell?.detailTextLabel?.text = product.localizedDescription + "(product.price)"

        

        

        return cell!

    }

    

    

    

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        

        // 1. 取出需要购买的商品

        let product = products[indexPath.row]

        

        

        // 2.0 p判断当前的支付环境, 是否可以支付

        if SKPaymentQueue.canMakePayments() {

            // 2. 购买商品

            // 2.1 根据商品, 开一个支付小票

            let payment = SKPayment(product: product)

            

            // 2.2 添加到支付队列, 开始进行购买操作

            SKPaymentQueue.defaultQueue().addPayment(payment)

            

            // 2.3 添加交易队列的监听者, 来监听交易状态

            SKPaymentQueue.defaultQueue().addTransactionObserver(self)

        }

        

        

        

    }

    

}

 

 

 

// 交易队列的监听者

extension ViewController: SKPaymentTransactionObserver {

    

    // 当交易队列里面添加的每一笔交易状态发生变化的时候调用

    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

        

        for transaction in transactions {

            

            switch transaction.transactionState {

            case .Deferred:

                print("延迟处理")

            case .Failed:

                print("支付失败")

                // 应该移除交易队列

                queue.finishTransaction(transaction)

            case .Purchased:

                print("支付成功")

                // 应该移除交易队列

                queue.finishTransaction(transaction)

            case .Purchasing:

                print("正在支付")

            case .Restored:

                print("恢复购买")

                // 应该移除交易队列

                queue.finishTransaction(transaction)

                

            }

            

            

        }

        

        

    }

    

    

    

}

 

 

// 商品请求代理

extension ViewController: SKProductsRequestDelegate {

    

    // 当请求完毕之后, 从苹果服务器获取到数据之后调用

    func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {

        

        // response

        // products: 可以被销售的额商品

        // invalidProductIdentifiers: 无效的商品ID

        print(response.products)

        print(response.invalidProductIdentifiers)

        

        

        

        // 给数据源进行赋值

        products = response.products

        

        

        

    }

}

原文地址:https://www.cnblogs.com/liuzhenjie/p/5417498.html