好的,现在一直在研究这个问题,尝试将本地HTML文件或URL加载到Swift for OS X而非iOS的Web视图中。
@IBOutlet weak var Webview: WebView!
func applicationDidFinishLaunching(aNotification: NSNotification?) {
// Insert code here to initialize your application
var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("pageName", ofType: "html"))
//var url = NSURL(string: "http://www.google.com")
var request = NSURLRequest(URL: url)
Webview.loadRequest(request)
}
所以我知道以上适用于iOS和UIKit,但它在OS X中不起作用它给出了错误Webview没有名为loadRequest的成员,它似乎没有任何加载URL的方法任何帮助表示赞赏!
答案 0 :(得分:7)
您必须以这种方式修改代码,以便首先将URL请求为字符串。这在swift中非常有效,无需缓存资产等:
@IBOutlet var webView: UIWebView!
var urlpath = NSBundle.mainBundle().pathForResource("index", ofType: "html");
func loadAddressURL(){
let requesturl = NSURL(string: urlpath!)
let request = NSURLRequest(URL: requesturl)
webView.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
loadAddressURL() //func above
// Do any additional setup after loading the view, typically from a nib.
}
答案 1 :(得分:6)
使用Webview.mainFrame.loadRequest(request)
代替Webview.loadRequest(request)
答案 2 :(得分:-1)
你必须使用它:
objweb.delegate = self
var path = NSBundle.mainBundle().bundlePath
var baseUrl = NSURL.fileURLWithPath("\(path)")
let bundle = NSBundle.mainBundle()
let pathhtml = bundle.pathForResource("pageName", ofType: "html")
let content = NSString.stringWithContentsOfFile(pathhtml) as String
objweb.loadHTMLString(content, baseURL: baseUrl)
self.view.addSubview(objweb)