ios - 编写完成处理程序的最佳方法是什么
问题描述
我目前正在我的应用程序中实现应用程序内购买,在恢复购买后,我想调用完成以执行向用户显示警报的操作。我是这样做的,发现一个帖子说它甚至可能不会被执行。我怎样才能正确地构建它。
func restoreIAPPurchases(completion: (() -> Void)) {
if !self.canMakePayments {
return
}
self.paymentQueue.restoreCompletedTransactions()
completion()
}
let alertController = UIAlertController.vy_alertControllerWithTitle(nil, message: "Restore will reprocess your existing subscription. You will not be charged", actionSheet: false)
alertController.addAction("Ok")
alertController.addActionWithTitle("Restore", style: .default) {
IAPService.shared.restoreIAPPurchases {
UIAlertController.vy_showAlertFrom(self, title: "Restore complete", message: "Successfully restored purchase")
}
}
alertController.presentFrom(self)
解决方案
“我是这样做的,发现一个帖子说它甚至可能不会被执行”
它可能不会被执行,因为您没有在所有路径上调用完成处理程序。
正如 Sh_Khan 在他的回答中提到的那样,您实际上并不需要一个完成处理程序,您需要使用委托方法来通知它何时完成以及它是否成功。但是您对特定代码的特殊问题是您没有在 if 语句中调用完成。
if !self.canMakePayments {
return
}
应该是
guard canMakePayments else {
completion()
return
}
在您拥有的代码中,如果 canMakePayments 为 false,那么您的完成代码将不会执行。
推荐阅读
- random - 在 ABAP CDS 视图中创建随机字符串
- python-3.x - 字典列表
- qt - 鼠标点击事件上的QtQuick pathCurve曲线
- android - 即使在设置了授权令牌后,也出现了来自改造的错误 401
- python - 从类方法调用实例方法,在python3中允许,在python 2中不允许?
- firebase - 在 Firebase Cloud Function 完成之前显示“加载”UI 的 UX 方法?
- php - laravel s3 在尝试屏蔽 URL 时从显示未知 SRC 的 API 获取图像
- c# - 直接在 Google-Chrome 中通过 URL 运行 WPF 应用程序
- sql - 当该字段完全等于“A”时,like '%A%' 语句和 in('A') 语句之间有什么区别吗?
- c# - 如何捕获 EF 异常