0
let langs = try? VNRecognizeTextRequest.supportedRecognitionLanguages(for: .fast, revision: 2)
                
let alertController = UIAlertController(title: "Select Language".localized, message: nil, preferredStyle: .actionSheet)
for lang in langs ?? []{
    let title = GlobalFunctions.getLanguageNameFrom(code: lang)
    let action = UIAlertAction(title: title, style: .default) { action in
        completion?(lang)
    }
    alertController.addAction(action)
}
let actionCancel = UIAlertAction(title: "Cancel".localized, style: .cancel, handler: nil)
alertController.addAction(actionCancel)

AppDelegate.shared.window?.rootViewController?.present(alertController, animated: true, completion: nil)

I received this error:

Thread 1: "Your application has presented a UIAlertController (<UIAlertController: >) of style UIAlertControllerStyleActionSheet from UISplitViewController (<UISplitViewController: >). 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 know I should add some code like this but its global function class I don't have a view

popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
 
HangarRash
  • 7,314
  • 5
  • 5
  • 32
user2296278
  • 528
  • 1
  • 4
  • 17
  • Your solution is fine. Just replace `self.view` with `rootViewController.view`. BTW - where is this global function defined? Why not make it part of a UIViewController extension? Then you don't need to go digging for a root view controller. – HangarRash Apr 25 '23 at 16:03
  • Do note that your code to show the alert has several other failure points. 1) It won't work on a modern app that uses scenes since the app delegate won't have a window. 2) Your code will fail if the root view controller is already presenting some other controller. – HangarRash Apr 25 '23 at 16:05
  • That is expected you need a source view. UI should only be presented from UI not from layers deep – lorem ipsum Apr 25 '23 at 16:10

0 Answers0