使用swift将本地html加载到UIWebView中

 凝笙儿 发布于 2022-12-14 11:10

这个让我今天早上疯狂.我想将一些本地html加载到Web视图中:

class PrivacyController: UIViewController {

    @IBOutlet weak var webView:UIWebView!

    override func viewDidLoad() {
        let url = NSURL(fileURLWithPath: "privacy.html")
        let request = NSURLRequest(URL: url!)
        webView.loadRequest(request)
    }
}

html文件位于我项目的根文件夹中,但位于组内.webview对我来说是空白的.任何想法都错了吗?我在xcode 6.1上并在我的iphone 6上运行这个例子.

7 个回答
  • 要检索应用程序资源的URL,您应该使用类的URLForResource方法NSBundle.

    斯威夫特2

    let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html") 
    

    斯威夫特3

    let url = Bundle.main.url(forResource: "privacy", withExtension: "html")
    

    2022-12-14 11:11 回答
  • // Point UIWebView
    @IBOutlet weak var webView: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        //load a file
        var testHTML = NSBundle.mainBundle().pathForResource("privacy", ofType: "html")
        var contents = NSString(contentsOfFile: testHTML!, encoding: NSUTF8StringEncoding, error: nil)
        var baseUrl = NSURL(fileURLWithPath: testHTML!) //for load css file
    
        webView.loadHTMLString(contents, baseURL: baseUrl)
    
    }
    

    2022-12-14 11:11 回答
  • Swift 33:)

    if let url = Bundle.main.url(forResource: "privacy", withExtension: "html") {
        webview.loadRequest(URLRequest(url: url))
    }
    

    2022-12-14 11:11 回答
  • 这对我有用:

    @IBOutlet weak var mWebView: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        mWebView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("fineName", ofType: "html")!)))
    
    }
    

    添加App Transport Security Settingsinfo.plist文件中的Dictionary类型.还为应用程序传输安全设置添加了类型和值的子键.Allow Arbitrary LoadsBooleanYES

    这是教程.

    EDITED

    对于Swift 3(Xcode 8)

    mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!)))
    

    2022-12-14 11:11 回答
  • Swift 3:类型安全

    @IBOutlet weak var webView: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Adding webView content
        do {
            guard let filePath = Bundle.main.path(forResource: "myFile", ofType: "html")
                else {
                    // File Error
                    print ("File reading error")
                    return
            }
    
            let contents =  try String(contentsOfFile: filePath, encoding: .utf8)
            let baseUrl = URL(fileURLWithPath: filePath)
            webView.loadHTMLString(contents as String, baseURL: baseUrl)
        }
        catch {
            print ("File HTML error")
        }
    }
    

    请记住:NS = Not Swift:]

    2022-12-14 11:11 回答
  • 将本地HTML文件添加到项目中并将该文件命名为home.html,然后使用NSURL对象创建NSURLRequest.在将请求传递给Web视图之后,它会将请求的URL加载到Web视图中,如果您不使用storyboard,请将uiwebview添加到视图控制器视图中,如下面的代码所示. 

     override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            let localfilePath = NSBundle.mainBundle().URLForResource("home", withExtension: "html");
            let myRequest = NSURLRequest(URL: localfilePath!);
            myWebView.loadRequest(myRequest);
            self.view.addSubview(myWebView)
        }
    

    有关更多参考,请参阅此http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/

    2022-12-14 11:11 回答
  • Swift 2.1版

    这种情况还包括编码

    // load HTML String with Encoding
    let path = NSBundle.mainBundle().pathForResource("policy", ofType: "html")
    do {
        let fileHtml = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
        webView.loadHTMLString(fileHtml as String, baseURL: nil)
    }
    catch {
    
    }
    

    2022-12-14 11:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有