swift加载h5页面空白

swift加载h5页面空白

problem

背景

  • xcode swift 项目,WebView方式加载h5页面
  • 本地h5地址是:http://localhost:5173/ 浏览器打开正常

Swift 加载h5:

  • 百度官网 加载正常
  • 本地h5页面 加载空白,没有报错
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // let myURL = URL(string:"https://www.baidu.com") // ok
        let myURL = URL(string:"http://localhost:5173/") // not working 页面空白
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }}

reason

本地是http服务,需要加个配置

solution

用源码方式打开 info.plist 文件

  • 右键-> open as -> source code
  • 添加以下配置
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Snipaste_2023-10-04_17-40-48

猜你喜欢

转载自blog.csdn.net/qubes/article/details/133557559