Using swift3 with Xcode8
Below is mu view controller.swift
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
YahooWebview.delegate = self
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
func webViewDidStartLoad(YahooWebview: UIWebView) {
print("show indicator")
activity.startAnimating()
}
func webViewDidFinishLoad(YahooWebview: UIWebView) {
activity.stopAnimating()
}
}
From my log in Xcode, it print "show indicator" then stop and then show something like the picture below.
Seems like something wrong with code below
activity.startAnimating()
Can anyone help?
Try below code for load site in web view
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
YahooWebview.delegate = self
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
func webViewDidStartLoad(_ webView: UIWebView) {
activity.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView) {
activity.stopAnimating()
}
}
Related
before posting this I was looking on the internet how to implement from cocoa pods MBCircularProgressBarView in iOS Swift WKWebKit to track progress of loading website.
I have tried with combination of some other progress bar codes but it didn't work.
I have tried to implement self.progressView.value = 0 in viewDidLoad
and
UIView.animate(withDuration: 1.0){
self.progressView.value = 100
}
in
didFinish navigation
which shows circular bar and animate but doesn't show proper loading progress.
Any idea how to make this work.
Make a class class WebpageViewController: UIViewController, UIWebViewDelegate containing:
#IBOutlet weak var activitySpinner: UIActivityIndicatorView!
#IBOutlet weak var activityLabel: UILabel!
override func viewDidAppear(_ animated: Bool)
{
activitySpinner.tintColor = R.color.YumaRed
activitySpinner.hidesWhenStopped = true
activityLabel.text = "Loading..."
webView.loadRequest(URLRequest(url: URL(string: "http://...")!))
}
func webViewDidStartLoad(_ webView: UIWebView)
{
activitySpinner.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
activitySpinner.stopAnimating()
activityLabel.isHidden = true
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
{
activitySpinner.stopAnimating()
}
override func viewDidLoad()
{
super.viewDidLoad()
webView.delegate = self
}
How can I refresh/load all the contents of my .html page before the webview is opening/showing?
This is my code:
class ContactViewController: UIViewController {
#IBOutlet weak var AboutWebView: WKWebView!
let url = URLAddress()
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.myfakeurl.com/about.html")
let request = URLRequest(url: url!)
AboutWebView.load(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
How can i adjust that with my code?
Just hide the web view until it's fully loaded and display it in webView(_:didFinish:).
Note that you should conform your view controller to WKNavigationDelegate.
class ContactViewController: UIViewController, WKNavigationDelegate {
override func viewDidLoad() {
[...]
aboutWebView.navigationDelegate = self
aboutWebView.isHidden = true
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.isHidden = false
}
}
So I've got a button which I'm trying to use as a means for the user to navigate to a specific URL (which it does), however the button is opening the url in safari, and i want it to be presented in my web view.
Here is my view controller.swift file:
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var myProgressView: UIProgressView!
#IBOutlet weak var Home_button: UIButton!
var theBool: Bool = false
#IBOutlet weak var refresh_button: UIButton!
#IBAction func go_home(_ sender: UIButton) {
UIApplication.shared.openURL(NSURL(string:"my url")! as URL)
}
var myTimer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
myTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(webViewDidStartLoad(_:)), userInfo: nil, repeats: true)
let websiteurl = URL(string: "another url")
let websiteurlrequest = URLRequest(url: websiteurl!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
webView.loadRequest(websiteurlrequest)
}
func webViewDidStartLoad(_ webView: UIWebView) {
//if let myProgressView.progress is not nil{
myProgressView?.progress += 0.045
//}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
let loadinglabel = self.view.viewWithTag(100)
loadinglabel?.removeFromSuperview()
myProgressView?.progress = 100
myProgressView?.removeFromSuperview()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func go() {
myProgressView?.progress += 0.005
}
}
How do I open the url in the web view on the button press, rather than in safari?
Thanks in advance!
Try this. Should work in Swift 3
#IBAction func go_home(_ sender: UIButton) {
yourWebView.loadRequest(URLRequest(url: URL(string: "http://www.google.com")!))
}
Change your button action method like this:
#IBAction func go_home(_ sender: UIButton) {
webView.loadRequest(websiteurlrequest)
}
and remove webView.loadRequest(websiteurlrequest) method from viewDidLoad. Then your webView load content when click on button.
Add the following code to the button action area
self.openWebURL(url: webUrl)
Function to load URL
func openWebURL(url : URL)
{
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)
}
SFSafariViewControllerDelegate should be added to class header like thise
class YourClassName: UIViewController, SFSafariViewControllerDelegate
Hope this helps...
How i can login many accounts of same site in different webView. For example i have Tab Bar Controller That contains three View Controllers and each View Controllers contain webView. And for example i embed stackoverflow url for webView in every class. How user can login to different accounts at the same time using these three webView? I've tried this but i just login one user at a time. Can any one please tell me how this is possible in (Swift or Objective-C)??
Thanks
class FirstViewController: UIViewController , UIWebViewDelegate{
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
webView.delegate = self
let requestURL = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: requestURL!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
class SecondViewController: UIViewController, UIWebViewDelegate{
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
webView.delegate = self
let requestURL = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: requestURL!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
I'm playing around with Swift to learn it and right now I'm having trouble getting the text from a UISearchBar
Right now my code looks like this:
import UIKit
class SecondViewController: UIViewController {
#IBOutlet var myWebView : UIWebView
#IBOutlet var adressbar: UISearchBar
override func viewDidLoad() {
super.viewDidLoad()
adressbar.showsScopeBar = true
var url = NSURL(string: "http://google.com")
var request = NSURLRequest(URL: url)
myWebView.scalesPageToFit = true
myWebView.loadRequest(request)
}
func searchBarSearchButtonClicked( searchBar: UISearchBar!) {
var url = NSURL(string: adressbar.text)
var request = NSURLRequest(URL: url)
myWebView.scalesPageToFit = true
myWebView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
In viewDidLoad you must set the delegate of the search bar to be receiving searchBarSearchButtonClicked from it:
import UIKit
class SecondViewController: UIViewController, UISearchBarDelegate
{
#IBOutlet var myWebView : UIWebView
#IBOutlet var adressbar: UISearchBar
override func viewDidLoad()
{
super.viewDidLoad()
adressbar.showsScopeBar = true
adressbar.delegate = self
var url = NSURL(string: "http://google.com")
var request = NSURLRequest(URL: url)
myWebView.scalesPageToFit = true
myWebView.loadRequest(request)
}
func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{
var url = NSURL(string: searchBar.text)
var request = NSURLRequest(URL: url)
myWebView.scalesPageToFit = true
myWebView.loadRequest(request)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
Simple (non-specific) answer for those arriving here via search:
If your outlet name for your UISearchBar is 'searchBar'...
#IBOutlet weak var searchBar: UISearchBar!
Then the text entered by the user in UISearchBar is called...
searchBar.text
Two common things Swift coders forget:
1. Make sure to add UISearchBarDelegate class...
ViewController: UIViewController, UISearchBarDelegate
Don't forget to identify your view class as the delegate...
Inside viewDidLoad you could write...
searchBar.delegate = self
If you forget #1 above you won't be able to auto-complete the methods you'll need to implement like...
func searchBarSearchButtonClicked(_ seachBar: UISearchBar) { your code }
... which is given to you by the delegate protocol.
1.First thing is to conform to the UISearchbarDelegate.
2.Set the search bar delegate to self.
You can find more on this here Swift iOS tutorial : Implementing search using UISearchBar and UISearchBarDelegate