SWIFT Can't Open Second URL in UIWebView with Second Button - swift

Boy am I a noob. Can't get a button to open a second URL in SWIFT. App opens, loads URL in WebView upon open. Now I want a button to open another url in the WebView when clicked. (Back button, go forward button and reload no prob)... Please help, thank you
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
var URLPath = "http://bandersnatchorlando.com/x19559"
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.
}
func loadAddressURL(){
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL:
requestURL)
webView.loadRequest(request)
}
#IBOutlet weak var reLoad: UIButton!
}

Assuming you already have the other button in your Storyboard and it is linked to an #IBAction then inside the action:
let url = NSURL(string:"http://myotherurl.com")
let request = NSURLRequest(URL:url)
webView.loadRequest(request)

Related

Need help to reload WKWebView from tabbed navigation in Swift

I'm trying to make this reload when a user clicks on the tab. I tried a simple reload but didn't work.
#IBOutlet weak var TabOne: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.google.com")
let urlRequest = URLRequest(url:url!)
TabOne.load(urlRequest)
}
Move the reload code into the viewWillAppear(_:)
as viewDidLoad() will only be called once in the view controller life cycle, while viewWillAppear(_:) will be called every time the view is going to appear on the screen.
This is the code that ended up working.
import UIKit
import WebKit
class FirstViewController: UIViewController {
#IBOutlet weak var TabOne: WKWebView!
override func viewWillAppear(_ animated: Bool) {
let url = URL(string: "https://www.google.com")
let urlRequest = URLRequest(url:url!)
TabOne.load(urlRequest)
}
}

Application starting in webview xcode 9

Pretty new to swift & xcode 9. I have my webview code working and displaying the proper link, but it opens the app with it. I have a few different controllers before it should be displayed (ultimately button triggered). When I have this code in, it doesn't change the default view controller as link, it simply loads the webview right away. Any help would be greatly appreciated!
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// 1
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
// 2
let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
toolbarItems = [refresh]
navigationController?.isToolbarHidden = false
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

webViewDidStartLoad and webViewDidFinishLoad not working

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

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)")

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
}