Load local html into UIWebView using swift

前端 未结 13 2014
耶瑟儿~
耶瑟儿~ 2020-11-28 22:27

This one is driving me crazy early this morning. I want to load some local html into a web view:

class PrivacyController: UIViewController {

    @IBOutlet w         


        
相关标签:
13条回答
  • 2020-11-28 23:05

    For swift 3 Use this:

    do
        {
            let testHTML = Bundle.main.path(forResource: "about", ofType: "html")
            let contents =  try NSString(contentsOfFile: testHTML!, encoding: String.Encoding.utf8.rawValue)
            let baseUrl = NSURL(fileURLWithPath: testHTML!) //for load css file
    
            mWebView.loadHTMLString(contents as String, baseURL: baseUrl as URL)
        }
        catch
        {
    
        }
    
    0 讨论(0)
  • 2020-11-28 23:06

    This is worked for me:

    @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")!)))
    
    }
    

    Added App Transport Security Settings with Dictionary type in info.plist file. Also added sub key Allow Arbitrary Loads for App Transport Security Settings with type Boolean and value YES.

    Here is tutorial.

    EDITED

    For Swift 3 (Xcode 8)

    mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!)))
    
    0 讨论(0)
  • 2020-11-28 23:07

    Swift version 2.1

    this case also include Encoding

    // 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 {
    
    }
    
    0 讨论(0)
  • 2020-11-28 23:10

    Swift 4.2, Xcode 10.1, WKWebView load HTML from file. UIWebView is deprecated.

    In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView.

    import WebKit
    
    @IBOutlet weak var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let localFilePath = Bundle.main.url(forResource: "document_terms_of_use", withExtension: "html")
        let request = NSURLRequest(url: localFilePath!)
        webView.load(request as URLRequest)
    }
    
    0 讨论(0)
  • 2020-11-28 23:11

    Swift 3: type safe

    @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")
        }
    }
    

    Keep in mind: NS = Not Swift :]

    0 讨论(0)
  • 2020-11-28 23:12

    Swift 3 with 3 lines :)

    if let url = Bundle.main.url(forResource: "privacy", withExtension: "html") {
        webview.loadRequest(URLRequest(url: url))
    }
    
    0 讨论(0)
提交回复
热议问题