addUIInterruptionMonitor的处理程序不会调用与相关的警报

private func acceptPermissionAlert() { _ = addUIInterruptionMonitor(withDescription: "") { alert -> Bool in if alert.buttons["Don't Allow"].exists { //doesnt get here second time alert.buttons.element(boundBy: 1).tapWhenExists() return true } return false } } 

这不适用于:

在这里输入图像说明

在应用程序的开始,它的作品完美时,通知的权限,但在这里…不工作。

你知道为什么吗?

加:

 app.tap() 

在方法的结尾。

这是因为你需要与应用程序进行交互,以处理程序触发。

添加中断监视器后,您应该继续与应用程序交互,就好像它没有出现。

另外请注意,在button标识符中有一个“智能报价”,而不是一个正常的撇号。

 let photosAlertHandler = addUIInterruptionMonitor(withDescription: "Photo Permissions") { alert -> Bool in if alert.buttons["Don't Allow"].exists { alert.buttons.element(boundBy: 1).tapWhenExists() return true } return false } // Do whatever you want to do after dismissing the alert let someButton = app.buttons["someButton"] someButton.tap() // The interruption monitor's handler will be invoked if the alert is present 

当警报出现后下一次交互发生时,将调用中断监视器的处理程序并处理警报。

当您认为已经完成时,您还应该移除中断监视器,否则将针对出现的任何其他警报调用它。

 removeUIInterruptionMonitor(photosAlertHandler)