ios - open urls externally
This commit is contained in:
parent
6abbbf11ff
commit
fb24537804
@ -19,11 +19,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import WebKit
|
@preconcurrency import WebKit
|
||||||
import AVFoundation
|
import AVFoundation
|
||||||
|
|
||||||
struct WebView: UIViewRepresentable {
|
struct WebView: UIViewRepresentable {
|
||||||
class Coordinator: NSObject, WKScriptMessageHandlerWithReply {
|
class Coordinator: NSObject, WKScriptMessageHandlerWithReply, WKNavigationDelegate {
|
||||||
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) {
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) {
|
||||||
guard let body = message.body as? [String : Any] else { return }
|
guard let body = message.body as? [String : Any] else { return }
|
||||||
guard let name = body["name"] as? String else { return }
|
guard let name = body["name"] as? String else { return }
|
||||||
@ -57,6 +57,15 @@ struct WebView: UIViewRepresentable {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
||||||
|
if let url = navigationAction.request.url, navigationAction.navigationType == .linkActivated {
|
||||||
|
UIApplication.shared.open(url)
|
||||||
|
decisionHandler(.cancel)
|
||||||
|
} else {
|
||||||
|
decisionHandler(.allow)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeCoordinator() -> Coordinator {
|
func makeCoordinator() -> Coordinator {
|
||||||
@ -70,6 +79,7 @@ struct WebView: UIViewRepresentable {
|
|||||||
configuration.userContentController.addScriptMessageHandler(Coordinator(), contentWorld: .page, name: "messageHandler")
|
configuration.userContentController.addScriptMessageHandler(Coordinator(), contentWorld: .page, name: "messageHandler")
|
||||||
configuration.setValue(true, forKey: "allowUniversalAccessFromFileURLs")
|
configuration.setValue(true, forKey: "allowUniversalAccessFromFileURLs")
|
||||||
let webView = WKWebView(frame: .zero, configuration: configuration)
|
let webView = WKWebView(frame: .zero, configuration: configuration)
|
||||||
|
webView.navigationDelegate = context.coordinator
|
||||||
//webView.isInspectable = true
|
//webView.isInspectable = true
|
||||||
return webView
|
return webView
|
||||||
}
|
}
|
||||||
@ -98,12 +108,12 @@ struct templateApp: App {
|
|||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ContentView()
|
ContentView()
|
||||||
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)) { _ in
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)) { _ in
|
||||||
audioPause()
|
audioPause()
|
||||||
}
|
}
|
||||||
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
||||||
audioResume()
|
audioResume()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user