Here's a working solution for overriding the menu. I would still like to get some html attributes, but don't know how to do that yet.
override func loadView() {
super.loadView()
let contentController = WKUserContentController();
let userScript = WKUserScript(
source: "redHeader()",
injectionTime: WKUserScriptInjectionTime.AtDocumentEnd,
forMainFrameOnly: true
)
contentController.addUserScript(userScript)
contentController.addScriptMessageHandler(
self,
name: "callbackHandler"
)
let config = WKWebViewConfiguration()
config.userContentController = contentController
self.webView = WKWebView(frame: self.view.frame, configuration: config)
self.webView.navigationDelegate = self
self.view = self.webView
let lpress = UILongPressGestureRecognizer(target: self, action: "webViewLongPressed:")
lpress.delegate = self
self.webView.scrollView.addGestureRecognizer(lpress)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func webViewLongPressed(sender:UILongPressGestureRecognizer!) {
longpress = true
if (sender.state == UIGestureRecognizerState.Ended) {
print("Long press Ended")
//This is where everything starts to happen
} else if (sender.state == UIGestureRecognizerState.Began) {
print("Long press detected.")
}
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if let myUrlStr : String = navigationAction.request.URL!.absoluteString {
if myUrlStr.lowercaseString.rangeOfString("/book/") != nil {
/* Do not allow links to be tapped */
var parts = myUrlStr.componentsSeparatedByString("/")
let id = Int(parts[4])
if navigationAction.navigationType == .LinkActivated && longpress == true {
decisionHandler(.Cancel)
let ac = actionMenu(self, id: id!, user_id: 1)
self.presentViewController(ac, animated: true) {
}
longpress = false
return
}
}
}
decisionHandler(.Allow)
}
//Build action sheet
func actionMenu(sender: UIViewController, id: Int, user_id: Int) -> UIAlertController {
let alertController = UIAlertController(title: "Title", message: "Some message.", preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
}
alertController.addAction(cancelAction)
let someAction = UIAlertAction(title: "Some action", style: .Default) { (action) in
//do something, call a function etc, when this action is selected
}
alertController.addAction(someAction)
return alertController
}