Default website when web browser launches xcode swift - swift

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.

Related

Why can I not connect to an IP with Port in WebKit / WebView?

I try to make a small iOS App which should open a single page app and then shows the content of a web site which is only accessible through an IP with Port
The code works with a standard URL like https://www.google.com but not with e.g. https://565.346.49.86:10000.
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://***.***.4*.*6:10000")
let request = URLRequest(url: url!)
webView.load(request)
}
}
I expect a login form of my home server.
It works for me when make the URL like this https://565.346.49.86:10000/

WKWebView shows a blank screen on OSX with no output

For some reason, the WKWebView just shows a blank screen with no errors
import Cocoa
import WebKit
class ViewController: NSViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
webView?.navigationDelegate = self
let url=URL(string: "https://www.google.com")
webView?.load(URLRequest(url: url!))
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
Like I said, no errors, and it just shows a blank screen.
The storyboard looks like this:
Main.storyboard
You need to allow your application to have outgoing connections.
In your Target go to Capabilities, enable the App SandBox and check the Outgoing Connections.

swift open website with a webview

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.

How do I enable links to open in default browser on WebView [duplicate]

This question already has answers here:
Open WebView URL in Browser
(2 answers)
Closed 7 years ago.
I'm learning OS X/Swift development and have loaded a webpage with contains links to other websites, how I open these links in the default browser. At the moment when clicking on the links nothing happens at all. This is my ViewController.swift contents:
import Cocoa
import WebKit
import Foundation
class ViewController: NSViewController, WebFrameLoadDelegate, WKNavigationDelegate {
#IBOutlet weak var webView: WebView!
override func viewDidLoad() {
super.viewDidLoad()
let URL = "https://test.mywebsite.com"
self.webView.frameLoadDelegate = self
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: URL)!))
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
I am not entirely clear what you are asking.
I think you are asking how to make links clicked inside a WebView open in the desktop browser (e.g. Safari)?
If this is what you are trying to achieve, you can use the WebPolicyDelegate to determine where a URL should open.
Eg.
import Cocoa
import WebKit
class ViewController: NSViewController, WebFrameLoadDelegate, WebPolicyDelegate {
#IBOutlet weak var webView: WebView!
let defaultURL = "http://www.apple.com/" // note we need the trailing '/' to match with our 'absoluteString' later
override func viewDidLoad() {
super.viewDidLoad()
self.webView.frameLoadDelegate = self
self.webView.policyDelegate = self
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: defaultURL)!))
}
func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) {
if let currentURL = request.URL {
if currentURL.absoluteString == defaultURL {
print("our base/default URL is being called - showing in WebView")
listener.use() // tell the listener to show the request
} else {
print("some other URL - ie. a link has been clicked - ignore in WebView")
listener.ignore() // tell the listener to ignore the request
print("redirecting url: \(currentURL.absoluteString) to standard browser")
NSWorkspace.sharedWorkspace().openURL(currentURL)
}
}
}
}
If this is not what you are asking, could you please edit your question to make it clearer.

Handle browser's Notification in Swift webview application

I have very little knowledge about Swift, xCode or Objective-C but I have read/watch a lot of tutorials and my only intention is to create a webview with notification in webkit wrapper such as this Cocoa application provided by xCode.
Currently I have my code that can pretty much run when I build it.
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WebView!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://irccloud.com"
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: urlString)!))
}
override func webView(sender: WebView!, runJavaScriptAlertPanelWithMessage message: String!, initiatedByFrame frame: WebFrame!) {
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
Now, if I access irccloud url directly I'm able to receive both sound and banner notification, but I'm wondering how can I make this notification able to display like native Mac app written in Swift?
This app able to do that, but it's in Objective-C https://github.com/jnordberg/irccloudapp