Reputation: 85
I have been trying to get webview with url(www.google.com) with various code but has no luck, also there is problem I'm face linking my webview with the view controller on the storyboard so will share the code so please help me out with this task.
import Cocoa
import WebKit
class ViewController: NSViewController {
@IBOutlet weak var webView: WebView!
//let webViewController = WebViewController()
override func viewDidLoad() {
super.viewDidLoad()
//let url = "http:www.google.com"
//self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: url)! as URL))
let url = NSURL (string: "http://www.google.com")
let requestObj = NSURLRequest(url: url! as URL)
webView.mainFrame.load(requestObj as URLRequest)
/*
//Open in browser
if let url = URL(string: "https://www.google.com"), NSWorkspace.shared().open(url) {
print("default browser was successfully opened")
}
*/
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
Upvotes: 0
Views: 737
Reputation: 1013
You need to set PolicyDelegate and override the method delegates.
override func viewDidLoad() {
super.viewDidLoad()
webView.policyDelegate = self
webView.mainFrame.loadRequest(NSURLRequest(URL: YOUR_URL_OBJECT))
}
Delegate methods
webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: webView:decidePolicyForNavigationAction:request:frame:decisionListener:
Upvotes: 1