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"])

        

        

    }

    

}

转载于:https://www.cnblogs.com/liuzhenjie/p/5417334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值