First fuck Cocoa Webkit !!!
runBeforeUnload ???
The WindowEventHandlers.onbeforeunload event handler property contains the code executed when thebeforeunloadis sent. This event fires when awindowis about to unload its resources. The document is still visible and the event is still cancelable.Apr 7, 2017 (https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload)
But Fuck. not working on my Cocoa Web App well.
Solution
1.implement WebUIDelegate (runBeforeUnloadConfirmPanelWithMessage)
2.handle close NSWindow (NSWindowDelegate -> windowShouldClose)
3.closeWebPage
try load special html like this :
```
let specialBank = Bundle.main.path(forResource:"bank", ofType:"html", inDirectory:nil)
webview.mainFrame.load(URLRequest(url: specialBank))
```
then it will fire 'runBeforeUnloadConfirmPanelWithMessage' (WebUIDelegate) if the current page need alert user confirm .and if user choice "Leave Page", you can close the window by yourself. if not it just do nothing and will stay current page. if the current page is common html page( mean not set 'onbeforeunload') ,webView will fire 'didStartProvisionalLoadFor' ,then you can handle webView.mainFrameURL is specialBank or not . if yes, just close window.
Demo here: WebViewCloseDemo
Good luck!
SKMacTalk
网友评论