Setting Accessibility Identifiers for UIAlertController in iOS

Setting Accessibility Identifiers for UIAlertController in iOS

10 November 2024 Stephan Petzl Leave a comment Tech-Help

When developing iOS applications, ensuring that UI elements are accessible for testing and accessibility purposes is crucial. One common requirement is setting accessibility identifiers for UIAlertController, which can help in identifying and interacting with alerts during UI testing. In this guide, we’ll explore methods to set accessibility identifiers for UIAlertController and its actions.

Creating and Presenting UIAlertController

Consider the following basic implementation of a UIAlertController:


  private class func showAlertWithTitle(title: String, message: String) {
      let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
      let action = UIAlertAction(title: "OK", style: .default) { action in
          alert.dismiss(animated: true, completion: nil)
      }
      alert.addAction(action)
      UIStoryboard.topViewController()?.present(alert, animated: true, completion: nil)
  }
  

In this example, we create a simple alert with a title, message, and an “OK” action. However, accessing this alert in UI tests by title can be cumbersome and unreliable. Setting a custom accessibility identifier can simplify this process.

Setting Accessibility Identifiers

A practical approach to set an accessibility identifier for the alert’s view is demonstrated below:


  let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  alert.view.accessibilityIdentifier = "custom_alert"
  alert.view.accessibilityValue = "\(title)-\(message)"

  alert.addAction(
      UIAlertAction(
          title: "ALERT_BUTTON_OK".localized,
          style: .default,
          handler: handler
      )
  )

  present(alert, animated: true)
  

By assigning an accessibility identifier to the alert’s view, you can easily reference this alert in your UI tests. Additionally, setting an accessibilityValue can further aid in verifying the alert’s content.

Alternative Method Using Private APIs

Another method involves using Apple’s private APIs, which might be less favorable due to potential App Store submission issues. This method involves accessing the _UIAlertControllerActionView:


  let alertView = UIAlertController(title: "This is Alert!", message: "This is a message!", preferredStyle: .alert)
  let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)

  alertView.addAction(okAction)

  self.present(alertView, animated: true) {
      let alertButton = action.valueForKey("__representer")
      let view = alertButton as? UIView
      view?.accessibilityIdentifier = "okAction_AID"
  }
  

While this approach is functional, it should be used with caution, particularly in production builds.

Conclusion

Setting accessibility identifiers on UIAlertController can significantly enhance the testability and accessibility of your iOS applications. By choosing the appropriate method, you can ensure that your alerts are easily identifiable and operable in automated tests.

Enhancing Mobile Testing with Repeato

For developers seeking a more efficient way to automate mobile app testing, Repeato offers a robust solution. As a no-code test automation tool, Repeato simplifies the creation and maintenance of automated tests for both iOS and Android applications. Its computer vision and AI capabilities make it particularly effective in scenarios where traditional tools like Appium may struggle with speed and stability. By leveraging Repeato, you can achieve faster test execution and streamlined test management, ensuring a smooth and efficient testing process.

Like this article? there’s more where that came from!