I'm trying to load a URL in my WebView in Swift as soon as the main dialog loads. I'm not sure what I'm doing wrong:
class WebViewController: NSViewController {
#IBOutlet var webView: WebView!
// constants
let webUrl = "https://www.google.com"
let webUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"
override func viewDidLoad() {
super.viewDidLoad()
let request = NSMutableURLRequest(URL: NSURL(string: webUrl)!)
request.setValue(webUA, forHTTPHeaderField: "User-Agent")
webView.mainFrame.loadRequest(request)
}
}
When debugging there are no errors or warnings. I think I linked the nib with the objects properly:
I've been playing around with delegations for a few hours now. The page simply doesn't load.
At first I attempted to use applicationDidFinishLaunching, but that went even worse, because the WebView was never finished being created and was nil.
Alright, so change your ViewController to look like this:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
var urlPath = "http://www.google.com"
func loadAddressUrl() {
let requestUrl = NSURL(string: urlPath)
let request = NSURLRequest(URL: requestUrl!)
webView.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadAddressUrl()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
And link the #IBOutlet to your web view controller. Example project available if you want.
Source: https://www.youtube.com/watch?v=Rva9ylPHi2w
Turns out I was pointing to the wrong WebView?!
In the hierarchy, it shows up like this:
I needed to use the one that's highlighted, not the inner one. The inner one was used when I control-dragged from Objects into the physical view object in the layout builder. Somehow dragging to this one fixed it.
Swfit 4 version of loadAddressUrl()
private func loadAddressUrl() {
let requestUrl = NSURL(string: url.text!)
let request = NSURLRequest(url: requestUrl! as URL)
webView.load(request as URLRequest)
}
Related
I am trying to load a local html based website into my macOS app and I am hitting issues, I have this working in iOS but not macOS.
My ViewController.swift looks like this
import Cocoa
import WebKit
class ViewController: NSViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
let webConfig = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfig);
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let htmlURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "website")!
webView.loadFileURL(htmlURL, allowingReadAccessTo: htmlURL.deletingLastPathComponent())
}
}
The App runs but then I get this output in the console in Xcode:
WebPageProxy::tryReloadAfterProcessTermination: process crashed and the client did not handle it, not reloading the page because we reached the maximum number of attempts
The documentation for what I am trying to do seems to be lacking I wonder if anyone can help me sort this out please.
After some more digging I needed to do 2 things:
Amend my viewDidLoad() code:
let htmlURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "polarix-website")!
webView.loadFileURL(htmlURL, allowingReadAccessTo: htmlURL.deletingLastPathComponent())
view = webView
Enable Outgoing Connections (Client) in the Signing & Capabilities
I have been creating my own web browser and have successfully done that in xcode. But then I wanted a certain url google for example to load when it first opens, now I have been having issues with this. If anyone can explain to me how to do this it would be great. I have tried multiple things in my ViewController class, but nothing seems to work.
Here's my code snippet, if there's something wrong with it could you tell me? :) thanks
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WebView!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://www.google.com/"
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: urlString)! as URL))
}
override var representedObject: Any? {
didSet {
}
}
}
The API changed quite a bit in Swift 3. Here is the code that works:
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WebView!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://www.google.com/"
let request = URLRequest(url: URL(string: urlString)!)
self.webView.mainFrame.load(request)
}
}
All the Foundation classes dropped the NS prefix (https://github.com/apple/swift-evolution/blob/master/proposals/0086-drop-foundation-ns.md) and because they changed the way parameters work, the load function changed quite a bit.
I have latest xcode beta, just trying to load a webpage inside an app.
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: "https://www.google.com");
let requestObj = NSURLRequest(URL: url!);
webView.loadRequest(requestObj);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
On this line: let requestObj = NSURLRequest(URL: url!);
I get following error:
"NSURL" is not implicitly convertible to "URL"; did you mean to use "as" to explicitly convert?
I removed the exclamation mark and I get this error:
Cannot convert value of type "NSURL?" to expected argument type "URL"
I've been trying variations on this code from all over the internet for the past four hours. I'm thinking latest swift just doesn't allow this anymore or something, because nothing works. I'm set to a 10.0 Deployment Target and a Xcode 8.0 compatible. But I've also tried earlier versions for both of those settings and I get the same error across all versions of both.
Yes, my webView on the storyboard is properly connected to the ViewController.swift.
In swift 3 you need to use URL instead of NSURL, so create url object like this.
if let url = URL(string: "https://www.google.com") {
webView.load(URLRequest(url: url))
}
Update for Swift 3.1:
let url = URL(string: "https://www.google.com")
let request = URLRequest(url: url!)
webView.load(request)
Notice the use of URL instead of NSURL and load instead of loadRequest.
My first app with swift... failing terribly.
I'm trying to open a website using webview and found this code online.
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://www.sourcefreeze.com")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
}
Well I get a fatal error: unexpectedly found nil while unwrapping an Optional value
Why should this value be null ?!
One of two things may be going on.
Your webView is not hooked up to a storyboard element.
Your URL object isn't be initialized
I think it's the former, but I've taken the liberty of making your code safer by optionally binding your NSURL object.
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
if let url = NSURL(string: "http://www.sourcefreeze.com") {
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}
}
Change webView.loadRequest(request) to webView?.loadRequest(request).
You need to unblock Transport Security which doesn't allow http loads. Try this.
I am just trying to create a simple webview inside a view controller.
Here is my code:
import UIKit
class test:UIViewController{
#IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var myString = "http://www.google.com"
let myURL = NSURL(string: myString)
let myReq = NSURLRequest(URL: myURL)
myWebView.loadRequest(myReq)
println(myWebView.loading)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm not getting any errors, simply a white screen. The loading check returns false. It seems like it should be the outlet connection, but I am sure I've made the proper connection. I ctrl-dragged the blue UIWebView element from storyboard into the class as the UIWebView! object and named it myWebView. The circle is filled in on the swift file indicating a connection, and they match up in the connections on the storyboard.
This code originally worked fine for me, but now it does not. I tried creating a completely new project and it still is not working. Adding the UIWebViewDelegate protocol to the class didn't work I've looked at the solutions on the following links, but they do not apply:
link1
link2
I'm out of ideas. Anyone got anything?
Thanks!
This is what you wan in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var myWeb = UIWebView(frame: CGRect(x: 40, y: 100, width: 240, height: 128))
var myString = "http://www.google.com"
let myURL = NSURL(string: myString)
let myReq = NSURLRequest(URL: myURL!)
myWeb.loadRequest(myReq)
view.addSubview(myWeb)
println(myWeb.loading)
}
Be careful, you have another web view (called myWebView) from the storyboard, as an outlet and that is not used. By writing the above code you create a new web view programmatically (called myWeb).