Ios Bundle.main.url(forResource:“hello”,扩展名为“html”)返回nil
我已经创建了hello.html文件。当我试图访问它时,url每次都返回nilIos Bundle.main.url(forResource:“hello”,扩展名为“html”)返回nil,ios,swift,Ios,Swift,我已经创建了hello.html文件。当我试图访问它时,url每次都返回nil override func viewDidLoad() { super.viewDidLoad() if let url = Bundle.main.url(forResource: "hello", withExtension: "html"){ if let htmlData = try? Data(contentsOf:url){
override func viewDidLoad() {
super.viewDidLoad()
if let url = Bundle.main.url(forResource: "hello", withExtension: "html"){
if let htmlData = try? Data(contentsOf:url){
let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
webView.load(htmlData, mimeType: "text/html", textEncodingName: "UTF-8", baseURL: baseURL)
}
}
// Do any additional setup after loading the view.
}
您的HTML文件未添加到目标中。明白了,谢谢男士:)您的HTML文件未添加到目标中。明白了,谢谢男士:)但HTML文件已在项目中。此答案仅在向项目中添加新文件时有用。如果在sanbox中添加html文件,请使用
FileManager
get file path,然后webview加载,可以,但html文件已在项目中。此答案仅在向项目中添加新文件时有用。如果在sanbox中添加html文件,请使用FileManager
get file path,然后使用webview加载即可