webViewDidStartLoad and webViewDidFinishLoad not working - swift

I am trying to check if the web view is loading and if its finish loading and when I try to search on it this is what I have gotten however it seems like it's not working is it because I have done something wrong or they change the code for it? I did set the delegate for the web view so I don't think the issue is with the delegate.
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://stackoverflow.com")
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ webView: UIWebView){
print("Webview started Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Webview did finish load")
}

You need to conform your class to UIWebViewDelegate and delegate managing of webView to your class:
class YourClass: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://stackoverflow.com")
//!!!!!!!
webView.delegate = self
//!!!!!!!
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ webView: UIWebView){
print("Webview started Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Webview did finish load")
}
}

You need to add "delegate" in your UIWebView
webView.delegate = self

Related

Grab loaded webpage URL? WKWebView macOS

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.urlText.stringValue = String(describing: webView.url!)
}
This does not work 100% of the time.
An example would be when I navigate to reddit. I click a thread and it changes URL which is fine, but if I click the reddit home button, it doesn't change URL to www.reddit.com even after it is 100% loaded.
Any help is appreciated.
What else is going on in your view controller? I'm not able to reproduce your problem with the code below. Have you tried this in an isolated project?
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var urlLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
let req = URLRequest(url:
URL(string: "https://reddit.com")!)
webView.load(req)
}
#IBAction func getUrlClicked(_ sender: Any) {
guard let url = webView.url else { return }
urlLabel.stringValue = url.absoluteString
print("url: \(String(describing: webView.url!))")
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}

How can i use open in new tab in swift ?(WEBKIT)

How can i use safari-open in new tab in my webkit app
I am creating a internet browser and i need that
Just like the photo
For example, I want to show the open in new tab option when I hold my finger on a link in Google
enter image description here
class ViewController: UIViewController, UITextFieldDelegate, WKNavigationDelegate {
#IBOutlet weak var backButton: UIButton!
#IBOutlet weak var forwardButton: UIButton!
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var urlTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
urlTextField.delegate = self
webView.navigationDelegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear( animated )
let urlString:String = "https://www.google.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let urlString:String = urlTextField.text!
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
textField.resignFirstResponder()
return true
}
#IBAction func backButtonTapped(_ sender: Any) {
if webView.canGoBack {
webView.goBack()
}
}
#IBAction func forwardButtonTapped(_ sender: Any) {
if webView.canGoForward {
webView.goForward()
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
backButton.isEnabled = webView.canGoBack
forwardButton.isEnabled = webView.canGoForward
urlTextField.text = webView.url?.absoluteString
}
}
Use this delegate method in bellow you last medhod :
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.targetFrame == nil {
if let url = navigationAction.request.url {
let app = UIApplication.shared
if app.canOpenURL(url) {
app.open(url, options: [:], completionHandler: nil)
}
}
}
decisionHandler(.allow)
}
is It oky ?

Swift Web Browser

I am extremely new to swift, and have no experience with objective-C, only partial C++.
I am trying to create a simple web browser with swift, this is the following code I have:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var containerView: UIView!
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string: "https://www.duckduckgo.com/")
var req = NSURLRequest(URL: url!)
self.webView!.loadRequest(req)
}
#IBAction func Searchresult(sender: AnyObject) {
url = NSURL(string: "https://www.duckduckgo.com/?q=\(sender)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Supposedly XCode says Use of unresolved identifier URL with #IBAction func Searchresult.
Once again I am greatly sorry if there is an obvious fix, I am extremely tired and not good at swift.
at first sight seems that you didn't declare url inside #IBAction, just put let url = NSURL(string: "https://www.duckduckgo.com/?q=\(sender)")

Navigation Controller Error

I have a navigation controller and I want the title to have a custom font. I have tried to do this but when it runs I get Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP.subcode=0x0)
Here is my code.
import UIKit
class PriceCheckSpreadsheetViewController: UIViewController {
#IBOutlet weak var SpreadsheetView: UIWebView!
#IBOutlet weak var Loading: UIActivityIndicatorView!
#IBOutlet weak var BackButton: UIBarButtonItem!
#IBOutlet weak var ForwardButton: UIBarButtonItem!
#IBOutlet weak var NaviBar: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let url = "http://www.backpack.tf/pricelist/spreadsheet"
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!)
SpreadsheetView.loadRequest(request)
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "TF2Build", size: 12)!]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ : UIWebView) {
Loading.startAnimating()
NSLog("Loading")
}
func webViewDidFinishLoad(_ : UIWebView) {
Loading.stopAnimating()
NSLog("Done")
if SpreadsheetView.canGoBack {
BackButton.enabled = true
}
else {
BackButton.enabled = false
}
if SpreadsheetView.canGoForward {
ForwardButton.enabled = true
}
else {
ForwardButton.enabled = false
}
}
#IBAction func Reload(sender: AnyObject) {
SpreadsheetView.reload()
}
#IBAction func Back(sender: AnyObject) {
SpreadsheetView.goBack()
}
#IBAction func Forward(sender: AnyObject) {
SpreadsheetView.goForward()
}
}

UIWebView centring page and allow navigation

I managed to have a website loaded in my UIWebView but I can't manage to center it.
I also would like to be able to "navigate" into the website. What am I doing wrong ?
here's the code :
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var WebView: UIWebView!
var URLPath = "http://google.fr"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
WebView.scalesPageToFit = true
WebView.keyboardDisplayRequiresUserAction = true
loadAdressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadAdressURL () {
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL: requestURL)
WebView.loadRequest(request)
}
}
And here's the IOS Simulator :
Rob.
Add this
override func viewDidAppear(animated: Bool) {
WebView.frame = UIScreen.mainScreen().bounds
WebView.center = self.view.center
}