2016-10-11 122 views
6
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 
    } 
} 

,爲此不工作:處理程序是不叫的警報相關照片

enter image description here

在應用程序的開始,它的工作完美而對於通知acepting許可,但在這裏...不起作用。

你知道爲什麼嗎?

+0

你是什麼意思?我應該在哪裏添加它? –

回答

9

添加:

app.tap() 

在該方法的結束。

這是因爲您需要與應用程序交互以處理程序觸發。

+2

或app.swipeUp()如果點擊會導致在您的應用程序中發生某些事情(並且您不希望它) – xaphod

+1

它適用於我。這是一種奇怪的...... 「你需要與應用程序交互才能觸發處理程序。」 是在哪個地方記錄? – cornr

+0

有沒有這個bug的雷達? –

1

添加中斷監視器後,您應該繼續與應用程序交互,就好像它沒有出現。

另請注意,您的按鈕標識符中包含「智能報價」,而不是常規撇號。

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)