-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from hotwired/blank-webview
Reload or recreate web view when its process is terminated
- Loading branch information
Showing
3 changed files
with
160 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Source/Turbo Navigator/Extensions/WKWebView+ebContentProcess.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Foundation | ||
import WebKit | ||
|
||
enum WebContentProcessState { | ||
case active | ||
case terminated | ||
} | ||
|
||
extension WKWebView { | ||
/// Queries the state of the web content process asynchronously. | ||
/// | ||
/// This method evaluates a simple JavaScript function in the web view to determine if the web content process is active. | ||
/// | ||
/// - Parameter completionHandler: A closure to be called when the query completes. The closure takes a single argument representing the state of the web content process. | ||
/// | ||
/// - Note: The web content process is considered active if the JavaScript evaluation succeeds without error. | ||
/// If an error occurs during evaluation, the process is considered terminated. | ||
func queryWebContentProcessState(completionHandler: @escaping (WebContentProcessState) -> Void) { | ||
evaluateJavaScript("(function() { return '1'; })();") { _, error in | ||
if let _ = error { | ||
completionHandler(.terminated) | ||
return | ||
} | ||
|
||
completionHandler(.active) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters