How to: use WKWebView on Mac with SwiftUI
A simple tutorial that solves the daily issues.
Recently, I am looking for extending the support of the app from iOS to macOS. However, the WebView
relies on the iOS UIViewRepresentable
which is not useable on mac. Here’s a quick step-by-step tutorial that helps solving the problem.
Get Started
Before coding, please ensure you have already added the target to macOS.
Coding
We need to change UIViewRepresentable
to NSViewRepresentable
. If you are using UIView
, change it to NSView
too. Below is a sample of code for you.
Now, let’s try running the project. Oops! A blank screen shows, with nothing happen?!
That is because the macOS project disabled network traffic by default. So, we need to enable this option via Signing & Capabilities
. You can find this in the project target.
If you cannot see App Sandbox
section, add + and select App Sandbox
.
Then, enable Outgoing Connections(Client)
in Network
.