应用内购买仅在首次应用启动时失败
Posted
技术标签:
【中文标题】应用内购买仅在首次应用启动时失败【英文标题】:In-app purchases fail only in first time app launch 【发布时间】:2021-01-22 12:46:28 【问题描述】:我的应用中有应用内购买。点击按钮后我的购买被激活。但是,如果我第一次打开应用程序并单击按钮,我会得到以下结果: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://***.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
如果我在调用后点击按钮,一切都会正常。
要调用它,我在AppDelegate
中使用以下行var store = MyAppProductStore()
,它看起来像这样:
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
var store = MyAppProductStore()
.etc
这是调用课程的最快方式吗?
【问题讨论】:
【参考方案1】:查看您链接到的代码,您必须先调用fetchStoreProducts
,然后在调用“self.product”之前确保产品已加载。可能应该更改原始代码以包含某种在产品加载请求完成时激活的回调。
【讨论】:
我明白这不被社区所欢迎。但是请您帮忙解决这个问题。我只是不知道该怎么办。谢谢! - ***.com/questions/69851479/…以上是关于应用内购买仅在首次应用启动时失败的主要内容,如果未能解决你的问题,请参考以下文章