swift - SwiftUI - 不清楚如何使用异步方法处理警报
问题描述
如果用户点击它,我必须在视图上显示警报。
我的警报取决于几种情况:
- 商品已购买。显示项目。
- 物品尚未购买。显示一个提醒,告诉用户必须购买该物品。此警报必须显示两个按钮,确定购买,取消取消。
- 用户点击购买商品。
- 购买成功,出示商品。
- 购买失败,显示错误。
我就是这样做的。
class AlertDialog {
enum SelectedType {
case none
case purchase
case mustBePurchased
case purchaseError
}
var selectedType:SelectedType = .none
}
struct FilteredListItem: View {
@State var showAlert: Bool = false
private var alertDialog:AlertDialog?
var body: some View {
Text(item.termLowerCase)
.font(fontItems)
.foregroundColor(.white)
.onTapGesture {
DispatchQueue.main.async {
appStoreWrapper.verifyPurchase(productID: item.package!)
{ // run if purchased
purchased = true
} runIfNotPurchased: {
purchased = false
alertDialog!.selectedType = .mustBePurchased
showAlert = true
}
}
}
.alert(isPresented: $showAlert) {
if alertDialog!.selectedType == .purchase {
appStoreWrapper.purchase(productID: item.package!) {
// run if purchased
purchased = true
} runIfPurchaseFailed: { (error) in
alertDialog!.selectedType = .purchaseError
appStoreWrapper.purchaseError = error
showAlert = true
}
} else if alertDialog!.selectedType == .purchaseError {
let primaryButton = Alert.Button.default(Text("OK")) {
showAlert = false
}
return Alert(title: Text(appStoreWrapper.makeString("ERROR")),
message: Text(appStoreWrapper.purchaseError),
dismissButton: primaryButton)
}
let dismissButton = Alert.Button.default(Text(appStoreWrapper.makeString("CANCEL"))) {
showAlert = false
}
let primaryButton = Alert.Button.default(Text("OK")) {
appStoreWrapper.purchase(productID: item.package!) {
// run if purchased
purchased = true
} runIfPurchaseFailed: { (error) in
appStoreWrapper.purchaseError = error
alertDialog!.selectedType = .purchaseError
showAlert = true
print(erro)
}
}
return Alert(title: Text(appStoreWrapper.makeString("ERROR")),
message: Text(appStoreWrapper.purchaseError),
primaryButton: primaryButton,
secondaryButton: dismissButton)
}
这是我的问题:修饰符.alert(isPresented: $showAlert)
期望Alert()
返回一个,对吗?但我有这些异步方法
appStoreWrapper.verifyPurchase(productID: item.package!)
{ // run if purchased },
runIfNotPurchased: { }
不能将任何内容返回给警报修饰符。我怎么做?我做的对吗?
解决方案
您的代码中有很多内容,您没有发布 appStoreWrapper 的代码,但这里有一些代码应该能够为您指明正确的方向。
供参考:
您可以将 Button 与 Action 一起使用,而不是将 Text 与 .onTapGesture 一起使用
.Alert 中的代码只能用于获取警报。您不应该在 .Alert 闭包中执行其他操作。
struct FilteredListItem: View { @State var showAlert: Bool = false private var alertDialog: AlertDialog? var body: some View { Button(action: { verifyItem() }, label: { Text("ITEM NAME") .foregroundColor(.white) }) .accentColor(.primary) .alert(isPresented: $showAlert, content: { getAlert() }) } func verifyItem() { // FUNCTION TO VERIFY ITEM HERE var success = true //appStoreWrapper.verifyPurchase... if success { // Handle success } else { alertDialog?.selectedType = .mustBePurchased showAlert.toggle() } } func purchaseItem() { // FUNCTION TO PURCHASE ITEM HERE var success = true //appStoreWrapper.purchase... if success { // Handle success } else { alertDialog?.selectedType = .purchaseError showAlert.toggle() } } func getAlert() -> Alert { guard let dialog = alertDialog else { return Alert(title: Text("Error getting alert dialog.")) } switch dialog.selectedType { case .purchaseError: return Alert( title: Text("Error purchasing item."), message: nil, dismissButton: .default(Text("OK"))) case .mustBePurchased: return Alert( title: Text("Items have to be purchased."), message: nil, primaryButton: .default(Text("Purchase"), action: { purchaseItem() }), secondaryButton: .cancel()) case .none, .purchase: return Alert(title: Text("Purchased!")) } } }
推荐阅读
- swift - Swift / Xcode - 展开可选值
- javascript - Javascript:调用方法来填充innerHTML不起作用,该方法是一种“字符串化”而不是被解释
- excel - 如何在具有唯一颜色的列中为 Excel 中的唯一值着色格式重复项?
- routes - 如何通过手动编写的过滤器在两个网络接口之间传递数据包?
- firebase - 如何将嵌套 Firestore 集合中的更改接收到单个颤动流中?
- ssh - ssh-add: 无法添加身份 xxx 代理拒绝操作
- php - 如果函数包含在 Wordpress/PHP 的模板部分中,get_template_part() 不会将样式排入队列
- pandas - 如何按日期过滤并使用关系代数(重构)计算这些日期?
- xml - 在 Oracle 中,如何从具有多个值的 XML/CLOB 字段中提取值?
- node.js - Storybook 似乎无法在节点 10.15.3 上运行