ios - 应用内购买仅在首次应用启动时失败
问题描述
我的应用中有应用内购买。点击按钮后我的购买被激活。但是,如果我第一次打开应用程序并单击按钮,我会得到以下结果:No products found with identifier
从这个函数:
func purchaseProduct(identifier:String) {
guard let product = self.product(identifier: identifier) else {
print("No products found with identifier: \(identifier)")
// fire purchase status: failed notification
delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .failed, error: PurchaseError.productNotFound, transaction: nil, message:"An error occured"))
return
}
//.etc
}
但如果我再次按下按钮,一切正常。此外,如果我关闭应用程序,再次打开并按下按钮 mu 购买工作正常。为什么这只发生在第一次应用启动时?以及如何解决?
我将此代码用于应用内购买https://stackoverflow.com/a/51688015
更新
有以下课程,我认为重点在于:
class MyAppProductStore: Store {
static let shared = MyAppProductStore()
// purchase processor
var itunes: iTunesStore = iTunesStore()
init() {
// register for purchase status update callbacks
itunes.delegate = self
validateProducts(MyProductIds.allIdentifiers())
}
/// This function is called during the purchase/restore purchase process with each status change in the flow. If the status is complete then access to the product should be granted at this point.
///
/// - Parameter status: The current status of the transaction.
internal func processPurchaseStatus(_ status: PurchaseStatus) {
switch status.state {
case .initiated:
// TODO: show alert that purchase is in progress...
break
case .complete:
if let productID = status.transaction?.payment.productIdentifier {
// Store product id in UserDefaults or some other method of tracking purchases
UserDefaults.standard.set(true , forKey: productID)
UserDefaults.standard.synchronize()
}
case .cancelled:
break
case .failed:
// TODO: notify user with alert...
break
}
}
}
extension MyAppProductStore: iTunesPurchaseStatusReceiver, iTunesProductStatusReceiver {
func purchaseStatusDidUpdate(_ status: PurchaseStatus) {
// process based on received status
processPurchaseStatus(status)
}
func restoreStatusDidUpdate(_ status: PurchaseStatus) {
// pass this into the same flow as purchasing for unlocking products
processPurchaseStatus(status)
}
func didValidateProducts(_ products: [SKProduct]) {
print("Product identifier validation complete with products: \(products)")
// TODO: if you have a local representation of your products you could
// sync them up with the itc version here
}
func didReceiveInvalidProductIdentifiers(_ identifiers: [String]) {
// TODO: filter out invalid products? maybe... by default isActive is false
}
}
如果我在调用它后单击按钮,一切都会正常工作。
要调用它,我在其中使用以下行var store = MyAppProductStore()
,AppDelegate
如下所示:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var store = MyAppProductStore()
.etc
这是最快的上课方式吗?
解决方案
查看您链接到的代码,您必须先调用fetchStoreProducts
,然后在调用“self.product”之前确保产品已加载。可能应该更改原始代码以包含某种在产品加载请求完成时激活的回调。
推荐阅读
- scripting - Windbg 脚本在 .foreach、.if、!for_each_local 中使用 as /c
- php - 如何选择多个表中的数据并按天查看?
- php - 如何动态绑定 SQL 参数?
- excel - 从忽略空白单元格的 Excel 工作表在 Outlook 中创建提醒
- api-key - Kong API 密钥策略
- c# - 我无法从 C# 连接到特定的 SQL Server 2008 数据库服务器
- r - 具有至少 n (>1) 个非缺失值的向量的平均值,R
- mongodb - 'mongo' 正在工作,但 'mongod' 在 CMD 中不起作用
- r - 如何在 1 个图中使用 CI 绘制 2 个均线图
- django - Django Model 的值为 NoneType 为什么?