Closed pbmarcano closed 7 years ago
I tried to implement this a few differents ways myself and couldn't get it to work either.
I tried downgrading to an older Turbolinks-iOS library but Xcode yelled at me because it was unable to build anything before Swift 3... Thanks Apple π...
I looked within the app of Basecamp 3 and it looks like Basecamp uses a data-confirm
when you are emptying the trash... and that seems to work fine on their iOS app.
So either the trash view is a default WKWebView or I am missing something.
@zachwaugh hate to put you on the spot but you seem to be the helpful contributor here recently! π
Like most problems I have, I was looking in the wrong spot.
Turns out, I wasn't building my links properly to be handled via XHR, and needed to add remote: true
to the rails link_to
method.
Your anchor tags need a data-remote="true"
attribute.
That took way too long. Sorry folks.
http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#link-to
Got this working thanks to @pbmarcano π
For reference, there are two changes to the TurbolinksDemo app make:
extension ApplicationController: WKUIDelegate {
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
print("webView:\(webView) runJavaScriptAlertPanelWithMessage:\(message) initiatedByFrame:\(frame) completionHandler:\(completionHandler)")
// Alert Controller
let confirm = UIAlertController(title: nil, message: message, preferredStyle: .alert)
confirm.addAction(UIAlertAction(title: "Okay", style: .default) { (action) in
completionHandler(true)
})
confirm.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action) in
completionHandler(false)
})
self.present(confirm, animated: true, completion: nil)
}
}
fileprivate lazy var session: Session = {
let session = Session(webViewConfiguration: self.webViewConfiguration)
session.delegate = self
session.webView.uiDelegate = self // This is the line to add
return session
}()
remote: true
and data: { confirm: "Are you sure?" }
to make them work. The remote: true
isn't there in regular scaffolds, hence why it wasn't originally working even with the above Swift code.
Per issue #88, I added the WKUIDelegate to my project and added:
The alert comes up just fine, but either button doesn't do anything. As if
completionHandler()
doesn't actually do anything.I tried adding this code to the TurbolinksDemo app with my backend and it also didn't work, but when adding it to a standard/Vanilla
WKWebView
it works just as the apple documentation suggests it should.