今天早上这使我发疯。我想将一些本地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上运行此示例。
要检索应用程序资源的URL,应使用URLForResource
类的NSBundle
方法。
Swift 2
let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html")
Swift 3
let url = Bundle.main.url(forResource: "privacy", withExtension: "html")
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 =不迅速:]
// 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)
}
Swift 3和3行:)
if let url = Bundle.main.url(forResource: "privacy", withExtension: "html") {
webview.loadRequest(URLRequest(url: url))
}
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 {
}
将本地HTML文件添加到您的项目中,并将该文件命名为home.html,然后使用NSURL对象创建NSURLRequest。在将请求传递到Web视图之后,它将把请求的URL加载到Web视图中,如果不使用情节提要,则将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/
这对我有用:
@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")!)))
}
在[[info.plist]]文件中添加App Transport Security Settings
类型的Dictionary
。还为应用程序传输安全设置添加了子键Allow Arbitrary Loads
,其类型为Boolean
,值为YES
。Here是教程。
已编辑
对于Swift 3(Xcode 8)mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!)))
Swift 4