如何在macOS中使用Swift打开指定URL的WebView窗口及关联问题求助
Hey there! Let’s work through your two WebView issues step by step—first getting that Storyboard connection sorted, then fixing the Google URL loading failure. I’ve dealt with both of these before, so let’s dive in.
1. Fixing Storyboard WebView-ViewController Connection Issues
If you can’t link your WebView to the ViewController, here’s what to check:
- First, confirm you’ve added the right WebView type to your Storyboard: use WKWebView (it’s the modern, supported replacement for UIWebView). Drag it from the Object Library onto your ViewController scene.
- Double-check your ViewController’s class assignment:
- Select the ViewController in Storyboard.
- Open the Identity Inspector (the third tab in the right sidebar).
- Make sure the Class field matches your actual ViewController class name (no typos—even a single wrong character breaks the connection).
- Try re-creating the outlet connection properly:
- Open the Assistant Editor (click the two-pane icon in the top-right of Xcode) to see your ViewController code alongside the Storyboard.
- Hold
Control, click and drag from the WebView in Storyboard to your ViewController code (inside the class, aboveviewDidLoad). - In the popup, set Connection to Outlet, name it
webView, and hit Connect.
- If all else fails, clean your project and restart Xcode: Go to
Product > Clean Build Folder(hold the Option key to see this option) then quit and re-open Xcode. Cached build data often causes weird Storyboard glitches.
2. Troubleshooting WebView Loading of
www.google.com Once your outlet is connected, let’s fix the loading issue. First, here’s a working WKWebView code snippet (make sure you’ve imported WebKit!):
import UIKit import WebKit class ViewController: UIViewController, WKNavigationDelegate { @IBOutlet weak var webView: WKWebView! override func viewDidLoad() { super.viewDidLoad() // Set the delegate to catch loading errors webView.navigationDelegate = self // Always use HTTPS for Google (it requires secure connections) guard let googleURL = URL(string: "https://www.google.com") else { print("Invalid URL") return } let request = URLRequest(url: googleURL) webView.load(request) } // This method will print errors to help debug func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { print("Loading failed: \(error.localizedDescription)") } }
Now, check these common reasons loading might fail:
- Missing HTTPS protocol: Google blocks non-HTTPS requests, so you must use
https://www.google.com(not justwww.google.com—without the protocol, the WebView doesn’t know how to load the resource). - Missing internet permission: On iOS 14+, you need to add the
NSInternetUsageDescriptionkey to yourInfo.plistfile. Add a string value explaining why your app needs internet access (e.g., "To load web content")—without this, iOS blocks network access silently. - App Transport Security (ATS) conflicts: By default, ATS allows valid HTTPS connections like Google’s, but if you’ve modified ATS settings in
Info.plist, make surewww.google.comisn’t blocked. If you haveNSAllowsArbitraryLoadsset toNO(default), that’s fine—Google’s SSL certificate is valid. - Delegate not set: The
webView.navigationDelegate = selfline is critical because it lets you see error messages (via thedidFailProvisionalNavigationmethod) that tell you exactly what’s going wrong. Without it, you won’t know why the load failed.
内容的提问来源于stack exchange,提问作者Sandeep
相关产品推荐
相关产品推荐

