Handler of addUIInterruptionMonitor is not called for Alert related to Photos
Solution 1
I'vs found that addUIInterruptionMonitor sometimes doesn't handle an alert in time, or until tests have finished. If it isn't working, try using Springboard, which manages the iOS home screen. You can access alerts, buttons, and more from there, and this is particularly useful for tests where you know exactly when an alert will show.
So, something like this:
`let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let alertAllowButton = springboard.buttons.element(boundBy: 1)
if alertAllowButton.waitForExistence(timeout: 5) {
alertAllowButton.tap()
}`
The buttons.element(boundBy:1)
will ensure you tap the button on the right, change 1 to 0 to tap the left, (because sometimes the ' in "Don't Allow"
causes a problem).
Solution 2
Add:
app.tap()
at the end of the method.
This is because you need to interact with the app for the handler to fire.
Solution 3
After adding the interruption monitor, you should continue to interact with the app as if it has not appeared.
Also note that you have a 'smart quote' in your button identifier, instead of a regular apostrophe.
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
When the next interaction happens after the alert appears, the interruption monitor's handler will be invoked and the alert will be handled.
You should also remove the interruption monitor when you think you're done with it, otherwise it will be invoked for any other alerts that appear.
removeUIInterruptionMonitor(photosAlertHandler)
Related videos on Youtube
Bartłomiej Semańczyk
iOS developer and keen enthusiast of and its development. Focused on clean code everytime while coding. I like Git very much:-) Swift is the only right language to programming on iOS. Implementing only latest solutions from Apple. Do not support old things. Tests are required, not optional:) #SOreadytohelp
Updated on June 04, 2022Comments
-
Bartłomiej Semańczyk almost 2 years
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 } }
and this doesnt work for:
In the beginning of the app, it works perfect while acepting permission for notifications, but here... doesnt work.
Do you know why?
-
xaphod over 7 yearsor app.swipeUp() if tapping would cause something to actually happen in your app (and you don't want it to)
-
cornr over 6 yearsIt works for me. This is kind of weird... "you need to interact with the app for the handler to fire." is that somewhere documented?
-
Wladek Surala over 6 yearsis there a radar for this bug?
-
Reinhard Männer over 6 years
app.tap()
taps to the middle of the screen, which might result in unwanted actions, as @xaphod pointed out. If you cannot useswipeUp()
either, maybe you can tap to a specific point on the screen, see here -
Aaron Brager almost 5 yearsThe actual button has the 'smart quote' character
’
not'
. Also your code produces the compiler error Value of type 'XCUIElement' has no member 'tapWhenExists'. -
Oletha almost 5 years@AaronBrager the
tapWhenExists()
method is one from the question's code, not a standard library function. I suggest you usetap()
instead, wrapped in a check forexists
. -
Axel Guilmin about 4 years
-
Simon about 4 years"When the next interaction happens", upvote from me for that one
-
Amresh over 3 years"When the next interaction happens" is indeed the key takeaway for me.
-
possen almost 3 yearsThe
addUIInterruptionMonitor
worked locally but on CI failed to trigger for access to camera. This was the only approach that seems to work. -
ChuckZHB about 2 yearsAdd this, but handler is still not triggered on Alert action sheet.
-
ChuckZHB about 2 years@possen THAT is true,
addUIInterruptionMonitor
just failed our tests on CI. I have replaced it with springboard too, and it fixes the failure on CI. -
Taylor Lindsay about 2 yearsgreat to hear :) @Bartłomiej Semańczyk I hope this fixed your issue?
-
Ting Yi Shih about 2 yearsAny interaction performed could trigger the handler, not just
app
. The key is: the handler would not be automatically triggered on the alert popping up. The handler will run only when the alert pops up AND after you fail to interact with some element.