UIWebView with Progress Bar

后端 未结 9 1326
攒了一身酷
攒了一身酷 2020-12-08 00:47

Hi I\'m new to programming and I\'m trying to make my first app for iPhones on Xcode. My app contains of a button which opens a UIWebView when pressed and loads up a homepag

相关标签:
9条回答
  • 2020-12-08 01:38

    I followed Wolflink's answer and found the same problem not having the progressView animated.

    After reading NSTimer Class Reference:

    https://developer.apple.com/library/Mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/occ/clm/NSTimer/timerWithTimeInterval:target:selector:userInfo:repeats:

    In Discussion you can read: "You must add the new timer to a run loop, using addTimer:forMode:. "

    so I added

    [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];

    after

    myTimer = [NSTimer timerWithTimeInterval:0.01667 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];

    It worked for me (As WolfLink recommended I did subclass UIProgressView )

    0 讨论(0)
  • 2020-12-08 01:38

    It's difficult (if even possible), since you would have to track all resources loaded by the site, but …

    I have one idea. It's more of a trick than a real solution, but I think even Apple uses this in Messages app :)

    1. When you start loading the page, begin an animation to 90% of the progress (let's say with duration of 1.5 seconds, maybe be different for Wi-Fi, LTE, 3G, …).
    2. When page loads in meantime, quickly animate the progress to 100%. Done!
    3. If the page takes more time to load, the progress bar will stop at 90% and will wait there. Frustrating moment when the user watches slow spinning indicator in status bar! And then finally, the page finish loading and (as in bullet 2.) you play quick animation to 100%. Done!

    I think we all know, that this is how Messages app works, since I don't believe sending SMS can be tracked with such accurate progress :)

    0 讨论(0)
  • 2020-12-08 01:44

    Some of these answers here are not completely correct, since they forgot to register the delegate for the webview. Unfortunately it is not enough to implement UIWebViewDelegate and override the two methods. You also have to set the delegate of the webview to self.

    class ViewController: UIViewController,UIWebViewDelegate {
    
        @IBOutlet weak var webView: UIWebView!
        @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
        @IBOutlet weak var myProgressView: UIProgressView!
    
        var myTimer = NSTimer()
        var theBool = Bool()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let url = NSURL(string: "https://www.youtube.com")
            let request = NSURLRequest(URL: url!)
            activityIndicator.hidesWhenStopped = true
            activityIndicator.startAnimating()
    
            // !
            webView.delegate = self // <--- important
            // ! 
    
            webView.loadRequest(request)
    
        }
    
        func timerCallback(){
            if theBool {
                if myProgressView.progress >= 1 {
                    myProgressView.hidden = true
                    myTimer.invalidate()
                }else{
                    myProgressView.progress += 0.1
                }
            }else{
                myProgressView.progress += 0.05
                if myProgressView.progress >= 0.95 {
                    myProgressView.progress = 0.95
                }
            }
        }
    
        func webViewDidStartLoad(webView: UIWebView) {
            activityIndicator.startAnimating()
            myProgressView.progress = 0
            theBool = false
            myTimer =  NSTimer.scheduledTimerWithTimeInterval(0.01667,target: self,selector: #selector(ViewController.timerCallback),userInfo: nil,repeats: true)
        }
    
        func webViewDidFinishLoad(webView: UIWebView) {
            activityIndicator.stopAnimating()
            theBool = true
        } 
    }
    

    This code refers to the answer @ZAFAR007

    0 讨论(0)
提交回复
热议问题