When you long press on a hyperlink in a WkWebview you get an action sheet. I want to override that action sheet with my own set of options when it is long pressed, but behave normally otherwise. I can get the the long press event, but I don't know how to:
One simple (but radical) way to cancel an action sheet is to override
presentViewController:animated:completion:
in your app's root view controller.
override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
guard let alertController = viewControllerToPresent as? UIAlertController,
alertController.preferredStyle == .actionSheet else {
// Not an alert controller, present normally
super.present(viewControllerToPresent, animated: flag, completion: completion)
return
}
// Create and open your own custom alert sheet
let customAlertController = UIAlertController(...)
super.present(customAlertController, animated: flag, completion: completion)
}
You can retrieve the link's URL from alertController.title
, and if you need to retrieve other attributes, I suggest you look at this commit to Firefox iOS, that uses a JS script handler to find the clicked element and send its attributes back to the app.
Also you'd need to write some logic to prevent cancelling any other alert sheet other than WKWebView's, that you app may use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With