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.
}
}
Related
First of all I'm new in swift. I have a WebView app for iOS device using swift language , can add button back to previous page ?
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com/")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
let statusBarFrame = UIApplication.shared.statusBarFrame
let statusBarView = UIView(frame: statusBarFrame)
self.view.addSubview(statusBarView)
statusBarView.backgroundColor = .systemBackground
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
I am using wkwebview to show a webpage but it is overlaying the toolbar. If I remove
view = webView Then the toolbar can be seen but not the webpage. Been going around in circles with this and tried several possible solutions in other threads but nothing is working. Here is my code...
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate{
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string:"website address")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
}
}
My code successfully loads the web page that I want it to; however, I'm having issues adding my activity indicator for while it is loading.
I am relatively new to swift, so I was reading other stackexchange examples. I created a UIActivityIndicator, set it in the override func, but it doesn't show up.
The webpage loads fine. Here is the runnable code.
import UIKit
import WebKit
class FeedbackWebViewController: UIViewController, WKNavigationDelegate {
let indicator = UIActivityIndicatorView(style: .gray)
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
indicator.center = webView.center
indicator.hidesWhenStopped = true
webView.addSubview(indicator)
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.apple.com")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
self.indicator.startAnimating()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
indicator.stopAnimating()
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
indicator.stopAnimating()
}
}
DONT override loadView. bad things may happen.
move code in didLoad. it should work.
try to put the related 3 lines to viewDidAppear rather than viewDidLoad. It works for me.
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)
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
}