-2

I've an iOS app with a WKWebview and when I click on a link with an alert popup the app crash, redirecting to AppDelegate with message:

Your application has presented a UIAlertController (<UIAlertController: 0x7f916a840a00>) of style UIAlertControllerStyleActionSheet from Kronos_Sport.ViewController (<Kronos_Sport.ViewController: 0x7f9168808c00>). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.

I'm not sure what should I add to my ViewController to fix this issue

EDIT: I've added that in viewDidLoad but it still crashes

    self.popoverPresentationController?.delegate = self
    
    self.popoverPresentationController?.sourceView = self.view
    self.popoverPresentationController?.sourceRect = self.view.bounds
    self.popoverPresentationController?.permittedArrowDirections = []
  • Actually the error tells you quite detailed what you can/should do. – vadian Aug 31 '22 at 10:37
  • I've added UIPopoverPresentationControllerDelegate to my viewController and an empty func prepareForPopoverPresentation and in viewdidload popoverPresentationController?.delegate = self but it still crash – Camille Gallet Aug 31 '22 at 10:45
  • Implementing the methods is not sufficient. ***You must provide location information*** where the popover is supposed to be displayed. – vadian Aug 31 '22 at 10:47
  • Ok I've found this https://stackoverflow.com/a/27440957 but what is alertView – Camille Gallet Aug 31 '22 at 11:08
  • I've added in viewdidload `popoverPresentationController?.sourceView = self.view popoverPresentationController?.sourceRect = self.view.bounds popoverPresentationController?.permittedArrowDirections = []` but it still crashes – Camille Gallet Aug 31 '22 at 11:14

2 Answers2

0

Action sheets present as popovers in standard size class containers (iPads). You need to set the source view and rect in this view, so the system knows where to present the popover.

From the title, it sounds to me like you want to present an alert, not an action sheet. In that case, use the correct enum value when creating the UIAlertControllet object.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
-2

Issue solved, I'vent forgot that I already use alertcontroller vars in my code so before the present I've added

if let popoverController = alertController.popoverPresentationController {
        popoverController.sourceView = self.view
        popoverController.sourceRect = self.view.bounds
        popoverController.permittedArrowDirections = []
}

et voila