Cocoa Webkit runBeforeUnload fire

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)


Cocoa Webkit runBeforeUnload fire_第1张图片

2.handle close NSWindow (NSWindowDelegate -> windowShouldClose)


Cocoa Webkit runBeforeUnload fire_第2张图片

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!


Cocoa Webkit runBeforeUnload fire_第3张图片

SKMacTalk

你可能感兴趣的:(Cocoa Webkit runBeforeUnload fire)