UIAlertController have different DOM structure in iPhone and iPad

104 Views Asked by At

enter image description hereI am showing UIAlertController in iOS and same code executes for iPhone and iPad. So can anyone help with this. As I google but didn't find any thing to justify this. Below is the code I have used:-

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Button.Ok".localizedValue(), style: .destructive, handler: { _ in

})
let cancelAction = UIAlertAction(title: "Button.Cancel".localizedValue(), style: .default, handler: { _ in
    alert.dismiss(animated: true, completion: nil)
})
alert.accessibilityLabel = self.viewModel.deleteUserConfirmationAccessibilityId()
alert.addAction(okAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)

So the problem is this:- iPhone : "Remove all saved" text come under UIAView of the DOM Structure when doing automation testing for the UIAlertController in iPhone. iPad : "Remove all saved" text come under UIView of the DOM Structure when doing automation testing for the UIAlertController in iPad.

So why difference is coming in UIAlertController structure as iPhone iPad showing UIView and iPhone UIAView for the same code.

1

There are 1 best solutions below

0
Yogesh Tandel On

You have to add popoverpresentation controller for ipad. Please see below code

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Button.Ok".localizedValue(), style: .destructive, handler: { _ in

})
let cancelAction = UIAlertAction(title: "Button.Cancel".localizedValue(), style: .default, handler: { _ in
alert.dismiss(animated: true, completion: nil)
})
alert.accessibilityLabel = 
self.viewModel.deleteUserConfirmationAccessibilityId()
alert.addAction(okAction)
alert.addAction(cancelAction)
//self.present(alert, animated: true, completion: nil)


if(UIDevice.current.userInterfaceIdiom == .pad){
        // for iPAD support:
        alert.popoverPresentationController?.sourceView = self.view
        alert.popoverPresentationController?.sourceRect = CGRect(x:self.view.bounds.width / 2.0, y:150, width:1.0, height:1.0)
        self.present(actionSheet, animated: true, completion: nil)
}else{
        // for iPHONE support:
        let rootViewController: UIViewController = (UIApplication.shared.keyWindow?.rootViewController)!
        rootViewController.present(alert, animated: true, completion: nil)
}