WKWebView 没有在 swift 中加载特定的 url

发布于 2025-01-09 04:18:00 字数 88 浏览 4 评论 0原文

无法仅在 WKWebView 中快速加载“https://dev-react.ndh01.com”此网址。但它正在浏览器和 Android Webview 中加载

Unable to load "https://dev-react.ndh01.com" this url only in WKWebView in swift. But it is loading in browser and Android Webview

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2025-01-16 04:18:00

您没有做任何错误,从技术上讲,您的网页加载正确,但该网站并未针对所有屏幕尺寸进行优化,无论您使用模拟器还是物理设备,您都可以通过旋转测试设备来验证这一点

import UIKit
import WebKit

class WebViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    
    

    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true
    preferences.javaScriptCanOpenWindowsAutomatically = true
    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences
    let webview = WKWebView(frame: .zero, configuration: configuration)
    view.addSubview(webview)
    webview.load(URLRequest(url: URL(string: "https://dev-react.ndh01.com")!))
    
    webview.translatesAutoresizingMaskIntoConstraints = false
    
    webview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    webview.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    webview.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    webview.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    
    
}


}

如果您使用模拟器,请尝试按Cmd + 右/左箭头将设备旋转至横向,

您将看到一个显示网页的屏幕

横向图像显示网页

you are not doing anything wrong and technically your webpage is loading properly, but this website is not optimized for all screen sizes, you can verify this by rotating your test device whether you using a simulator or physical device

import UIKit
import WebKit

class WebViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    
    

    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true
    preferences.javaScriptCanOpenWindowsAutomatically = true
    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences
    let webview = WKWebView(frame: .zero, configuration: configuration)
    view.addSubview(webview)
    webview.load(URLRequest(url: URL(string: "https://dev-react.ndh01.com")!))
    
    webview.translatesAutoresizingMaskIntoConstraints = false
    
    webview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    webview.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    webview.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    webview.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    
    
}


}

if you are using a simulator try pressing Cmd + right/left arrow to rotate the device to landscape

you will find a screen showing your web page

landscape image shows you webpage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文