美文网首页
Cocoa Webkit runBeforeUnload fir

Cocoa Webkit runBeforeUnload fir

作者: IMSk | 来源:发表于2017-05-05 22:28 被阅读56次

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

相关文章

网友评论

      本文标题:Cocoa Webkit runBeforeUnload fir

      本文链接:https://www.haomeiwen.com/subject/kggstxtx.html