Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetworkActivityIndicator not working

I'm trying to make an networkActivityIndicator (in SWIFT), but nothing happening, I have do some test but nothing. This ActivityIndicator is normally linked to the WebView.

class ViewController: UIViewController, UISearchBarDelegate, UIWebViewDelegate {

@IBOutlet weak var WebView: UIWebView!

@IBOutlet weak var SearchBar: UISearchBar!


//WebView
func WebViewDidStartLoad(WebView : UIWebView) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    println("Chargement")
}

func WebViewDidFinishLoad(WebView : UIWebView) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    println("Fin chargement")
}

//SearchBar
func searchBarSearchButtonClicked(SearchBar: UISearchBar!) {

    SearchBar.resignFirstResponder()
    var text = SearchBar.text
    text = text.stringByReplacingOccurrencesOfString(" ", withString: "+");
    var url = NSURL(string: "https://www.google.fr/#q=".stringByAppendingString(text));
    var req = NSURLRequest(URL:url!)
    WebView.delegate = self
    self.WebView!.loadRequest(req)

}

1 Answers

Here is the complete working code for you:

import UIKit
import WebKit

class ViewController: UIViewController, UISearchBarDelegate, UIWebViewDelegate {


@IBOutlet weak var searchBar: UISearchBar!

@IBOutlet weak var webView: UIWebView!

func searchBarSearchButtonClicked(searchBar: UISearchBar!) {

    searchBar.resignFirstResponder()
    var text = searchBar.text
    text = text.stringByReplacingOccurrencesOfString(" ", withString: "+");
    var url = NSURL(string: "http://google.com/search?q=".stringByAppendingString(text));
    var req = NSURLRequest(URL:url!)
    self.webView!.loadRequest(req)
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.searchBar.delegate = self
    webView.delegate = self
}
func webViewDidStartLoad(webView: UIWebView){
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    println("Chargement")
}
func webViewDidFinishLoad(webView: UIWebView){
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    println("Fin chargement")
   }
}

Hope it helps.

like image 141
Dharmesh Kheni Avatar answered Apr 25 '26 20:04

Dharmesh Kheni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!