Trombone0904
Trombone0904

Reputation: 4268

Show activity indicator while website is loading

i searchin for a ios 8 swift 2 solution for showing an activity indicator while my website is loading in the background.

at the moment, i have this code:

    override func viewDidLoad() {
    super.viewDidLoad()
    var url = "http://www.google.de"
    let requestURL = NSURL(string:url)
    let request = NSURLRequest(URL: requestURL!)
    webView.loadRequest(request) }

Problem is, the view controlle is the whole time white while the webseite is loading. i hope you can help me :)

Upvotes: 2

Views: 3648

Answers (4)

Mohamed Mo Kawsara
Mohamed Mo Kawsara

Reputation: 4688

If you wish, refer to this answer

https://stackoverflow.com/a/65136127/2131039


Here it's anyways (just in case if it got deleted)

Tested in Swift 5, you can use the following simple extension:

// Activity Indicator Extension
var vSpinner : [UIView] = []

extension UIViewController {
    func showSpinner(onView : UIView) {
        let spinnerView = UIView.init(frame: onView.bounds)
//        spinnerView.backgroundColor = UIColor.init(red: 0.8, green: 0.8, blue: 0.8, alpha: 0.9)
        let ai = UIActivityIndicatorView.init(style: .medium)
        ai.startAnimating()
        ai.center = spinnerView.center
        
        DispatchQueue.main.async {
            spinnerView.addSubview(ai)
            onView.addSubview(spinnerView)
        }
        
        vSpinner.append(spinnerView)
    }
    
    func removeSpinner() {
        DispatchQueue.main.async {
            vSpinner.popLast()?.removeFromSuperview()
        }
    }
}

Within your async task use these two handy boys:

showSpinner(onView: self.view) Remember to replace self.view with any kind of view you desire

self.removeSpinner() Remove the loading indicator:

Upvotes: 0

elahiammar
elahiammar

Reputation: 129

you can use MBProgressHUD library for loading indicator in swift, also you can check How to use MBProgressHUD with swift if it's working in your situation.

Upvotes: 1

Divjot Singh Sandhu
Divjot Singh Sandhu

Reputation: 21

Drag the Activity Indicator View to the ViewController class and add the following code after this line // webView.loadRequest(request) // or which ever line loads the url.

activityIndicatorView.startAnimating()

To stop animation use

activityIndicatorView.stopAnimating()

You can also use a stop and start button or any other Action .

Upvotes: 2

dorian
dorian

Reputation: 847

You need to add an activity indicator to the view of the contoller. Check out this tutorial: http://www.ioscreator.com/tutorials/activity-indicator-tutorial-ios8-swift

Upvotes: 1

Related Questions