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

Upv*_*ote 70 uiwebview ios swift

这个让我今天早上疯狂.我想将一些本地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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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

rin*_*aro 77

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

斯威夫特2

let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html") 
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

let url = Bundle.main.url(forResource: "privacy", withExtension: "html")
Run Code Online (Sandbox Code Playgroud)

  • 吸引人的一点是它必须是“privacy”而不是“privacy.html”。这就是它对我不起作用的原因。 (2认同)

Séb*_*EMY 24

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")
    }
}
Run Code Online (Sandbox Code Playgroud)

请记住:NS = Not Swift:]


Jup*_*Cls 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)

}
Run Code Online (Sandbox Code Playgroud)


MBH*_*MBH 11

Swift 33:)

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


Sru*_*Suk 9

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 {

}
Run Code Online (Sandbox Code Playgroud)


Bha*_*asu 7

将本地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)
    }
Run Code Online (Sandbox Code Playgroud)

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


Abd*_*fiz 5

这对我有用:

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

}
Run Code Online (Sandbox Code Playgroud)

添加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")!)))
Run Code Online (Sandbox Code Playgroud)