StoreKit

 

import UIKit

import StoreKit

 

class ViewController: UITableViewController {

 

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

        

        didSet {

            tableView.reloadData()

        }

        

    }

    

    

    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()

 

        }

     

    }

 

 

}

 

 

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

        cell?.textLabel?.text = product.localizedTitle

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

        

        

        return cell!

    }

    

    

}

 

 

extension ViewController: SKProductsRequestDelegate {

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

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

        

        // response 

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

        // invalidProductIdentifiers: 无效的商品ID

        

        

        

        print(response.products)

        

        products = response.products

        

        print(response.invalidProductIdentifiers)

        

    }

}

 

 

 

----------------------------------

//  获取需要销售的商品

import Foundation

 

class LZJDataTool: NSObject {

 

    

    class func getGoodsList(result: (Set<String>)->()) {

        

        // 先我们自己的服务器发送网络请求, 获取商品数据

        

        

        result(["com.shiyi.zidan", "com.shiyi.jiguanqiang", "com.shiyi.jiguanqiang2", "com.shiyi.jiguanqiang3"])

        

        

    }

    

}

posted @ 2016-04-21 15:58  suifenglangzi  阅读(275)  评论(0编辑  收藏  举报