闽公网安备 35020302035485号
在以前,测试内购不是一件容易的事情,需要开沙盒账号,打各种测试包之类的。从 Xcode 14 开始,苹果推出了一个 StoreKit 配置文件,可以支持在 Xcode 中测试 App 内购买项目。还能够在不连接到 App Store 服务器的情况下购买产品,无需互联网连接,并且还能测试交易失败等各种边缘情况。
今天就来聊聊这个功能。







import StoreKit
class ViewController: UIViewController, SKProductsRequestDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
// 配置请求
let req = SKProductsRequest(productIdentifiers: ["ios.product1"])
req.delegate = self
// 开始请求商品
req.start()
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
guard let product = response.products.first else {
return
}
// 堆代码 duidaima.com
// 商品请求成功,渲染出来
DispatchQueue.main.async {
let label = UILabel()
label.text = "商品ID:\(product.productIdentifier)\n商品名:\(product.localizedTitle)\n价格:\(product.price)"
label.numberOfLines = 0
label.textAlignment = .center
self.view.addSubview(label)
label.sizeToFit()
label.center = self.view.center
}
}
}
我这里只是简单的写了获取商品的代码,关于购买的部分,以后专门写一篇给大家介绍。最后运行代码,就可以成功的在模拟器上看到商品信息了: