这个问题通常出现在iOS App中的应用内购买时,需要刷新应用的购买凭证。为了解决这个问题,我们建议在后台任务超时之前,定期刷新购买凭证。
下面是一个代码示例,可以在应用启动时启动一个后台任务,并使用 SKReceiptRefreshRequest 在 App Store 中刷新购买凭证:
// 定义后台任务的处理函数
func startBackgroundTask() {
let receiptRefreshRequest = SKReceiptRefreshRequest()
receiptRefreshRequest.start { (error) in
if error != nil {
print("Could not refresh receipt: \(error!.localizedDescription)")
} else {
print("Receipt refresh successful.")
}
}
}
// 在应用启动时启动后台任务
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ...
// 如果设备支持后台任务,则启动后台任务
if application.backgroundRefreshStatus == .available {
let timer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: true) { (_) in
let backgroundTimeRemaining = UIApplication.shared.backgroundTimeRemaining
if backgroundTimeRemaining < 60.0 {
startBackgroundTask()
}
}
timer.fire()
}
// ...
return true
}
在上面的代码中,我们首先定义了一个名为 startBackgroundTask 的函数,用于执行购买凭证的刷新操作。然后,在应用启动时,我们使用 Timer 定时监测后台任务的时间剩余量,如果剩余时间少于60秒,则启动一个后台任务来刷新购买凭证。
使用上面的代码示例,我们可以轻松地解决'后台任务9(‘SKReceiptRefreshRequest’)已经创建超过30秒?”的问题。
上一篇:背景去除深度学习