Load local html into UIWebView using swift

前端 未结 13 2015
耶瑟儿~
耶瑟儿~ 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:13

    Here is succinct version for Swift 4

    1) Add import import WebKit

    2) Add WebKit.framework in your project

    @IBOutlet weak var webView: WKWebView!
    
    if let filePath = Bundle.main.url(forResource: "FILE_NAME", withExtension: "html") {
      let request = NSURLRequest(url: filePath)
      webView.load(request as URLRequest)
    }
    
    0 讨论(0)
  • 2020-11-28 23:17
    // 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)
    
    }
    
    0 讨论(0)
  • 2020-11-28 23:20

    This worked for me (Xcode 8, Swift 3)

    @IBOutlet weak var webViewer: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let localfilePath = Bundle.main.url(forResource: "homeInfo", withExtension: "html");
        let myRequest = NSURLRequest(url: localfilePath!);
        webViewer.loadRequest(myRequest as URLRequest);
        self.view.addSubview(webViewer)
    }
    
    0 讨论(0)
  • 2020-11-28 23:22

    swift 4.2 & 5:

    let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768))
    let url = URL(string: PERS_Path)
    webView?.navigationDelegate = self
    webView?.uiDelegate = self
    webView!.loadFileURL(url!, allowingReadAccessTo: url!)
    self.view. addSubview(webView)
    

    don't forget to add this WKNavigationDelegate, WKUIDelegate

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

    You can load html string or local html file in UIWebView.

    HTML string:

    func loadHtmlCode() {
        let htmlCode = "<html><head><title>Wonderful web</title></head> <body><p>wonderful web. loading html code in <strong>UIWebView</strong></></body>"
        webView.loadHTMLString(htmlCode, baseURL: nil)
    }
    

    HTML file:

    func loadHtmlFile() {
        let url = NSBundle.mainBundle().URLForResource("contactus", withExtension:"html")
        let request = NSURLRequest(URL: url!)
        webView.loadRequest(request)
    }
    

    Details can be found here: http://webindream.com/load-html-uiwebview-using-swift/

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

    Add the local HTML file into your project and name that file as home.html, then create the NSURLRequest using NSURL object. After that passing the request to web view it will load the requested URL into web view and if you are not using storyboard add the uiwebview into view controller view like the below code. 

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

    For more reference please refer this http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/

    0 讨论(0)
提交回复
热议问题