diff --git a/share/ios/Sources/SharePlugin/SharePlugin.swift b/share/ios/Sources/SharePlugin/SharePlugin.swift index 73aa61b2b..234c5f286 100644 --- a/share/ios/Sources/SharePlugin/SharePlugin.swift +++ b/share/ios/Sources/SharePlugin/SharePlugin.swift @@ -64,12 +64,28 @@ public class SharePlugin: CAPPlugin, CAPBridgedPlugin { } } - if self?.bridge?.viewController?.presentedViewController != nil { - call.reject("Can't share while sharing is in progress") - return + // Present from the topmost presented view controller so the share sheet appears + // above any view controller the app has presented over the webview. With nothing + // presented this resolves to the bridge view controller, i.e. unchanged behaviour. + // A share is only in progress when a share sheet is already presented. + var presenter = self?.bridge?.viewController + while let presented = presenter?.presentedViewController { + if presented is UIActivityViewController { + call.reject("Can't share while sharing is in progress") + return + } + presenter = presented + } + // `setCenteredPopover` anchors to the bridge view controller's view, which is not in + // the presenter's hierarchy when presenting from a different view controller. Anchor + // the popover to the presenting view controller's own view instead, centered and + // without an arrow, matching `setCenteredPopover` behaviour. + if let popover = actionController.popoverPresentationController, let presenterView = presenter?.view { + popover.sourceView = presenterView + popover.sourceRect = CGRect(x: presenterView.bounds.midX, y: presenterView.bounds.midY, width: 0, height: 0) + popover.permittedArrowDirections = [] } - self?.setCenteredPopover(actionController) - self?.bridge?.viewController?.present(actionController, animated: true, completion: nil) + presenter?.present(actionController, animated: true, completion: nil) } } }