I'm writing a diagnostic app for iOS that loads a predetermined set of webpages and records the time it takes for the page to render on the device.

Posted by user1754840 on Stack Overflow See other posts from Stack Overflow or by user1754840
Published on 2012-10-24T04:56:51Z Indexed on 2012/10/24 5:00 UTC
Read the original article Hit count: 83

Filed under:
|
|
|

I'm writing a sort of diagnostic app for iOS that opens a predetermined list of websites and records the elapsed time it takes each to load. I have the app open a UIWebView within a ViewController.

Here are the important bits of the ViewController source:

- (void)viewDidLoad {
    [super viewDidLoad];
    DataClass *obj = [DataClass getInstance];
    obj.startOfTest = [NSDate date];

    //load the first webpage
    NSString *urlString = [websites objectAtIndex:obj.counter];  //assume firstWebsite is already instantiated and counter is initially set to zero
    obj.counter = obj.counter + 1;
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [obj.websiteStartTimes addObject:[NSDate date]];
    [webView loadRequest:request];
}

- (void)webViewDidFinishLoading:(UIWebView *)localWebView{
    DataClass *obj = [DataClass getInstance]; //gets 'global' variables

    if(!webView.loading){ 
        NSString *urlString = [websites objectAt:obj.counter];
        obj.counter = obj.counter + 1;
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [obj.websiteStartTimes addObject:[NSDate date]];
        [webView loadRequest:request];
    }

The problem with this code is that it seems to load the next website before the one before it has finished. I would have thought that both the call to webViewDidFinishLoading AND the if statement within that would ensure that the website would be done, but that's not the case. I've noticed that sometimes, a single website will invoke the didFinishLoading method more than once, but it would only enter the if statement once.

For example, if I have a list of ten websites, the webView would only really show the 3rd and the 6th website on the list and then indicate that it was "done" rendering them all.

What else can I do to ensure that a website is done loading completely and rendered to the screen before the app moves on to the next one?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c