In my iPad App I have a modal view (UIViewController
with modal presentation style UIModalPresentationPageSheet
)
Inside the view is a UIWeb
In Swift method :
override public func viewDidDisappear(animated: Bool) {
super.viewDidAppear(animated)
self.webView.loadHTMLString("", baseURL: nil)
}
thanks @alloc_iNit
Do integrate following code to sort out the problem.
-(void)viewWillDisappear:(BOOL)animated
{
[webView loadHTMLString:nil baseURL:nil];
}
Swift 4
// Halt anything in progress
(contentViewController as! WebViewController).webView.loadHTMLString("Yoink://", baseURL: nil)
needed in a windowController's windowShouldClose() method when I close a window
Also you can try to pause video:
NSString *script = @"var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };";
[webView stringByEvaluatingJavaScriptFromString:script];
I saw there are many suggestions with loading blank page. That is really bad approach.
It will cause your webview to be blank if, let say you push you another view on top of it.
Most simple solution is to reload the view with:
Swift
webView.reload()
Obj-C
[webView reload]
Use the following javascript in WKWebview to pause the video player in swift
wkWebView.evaluateJavaScript("var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };", completionHandler: nil)
to stop the video player use:
wkWebView.loadHTMLString("", baseURL: nil)