iOS measuring web page loading time

前端 未结 2 1788
谎友^
谎友^ 2021-01-06 09:55

I searched a lot but couldn\'t find the way to measure web page loading time with iOS. In the app, I want to show certain page loading time.. Is it possible with iOS sdk or

2条回答
  •  不思量自难忘°
    2021-01-06 10:39

    You can load a URL request and use NSDate to see how long it took...lets assume you use a UIWebView to show your page so to measure the loading time i would capture the time when the URL is requested and then in the delegate methods - (void)webViewDidFinishLoad:(UIWebView *)webView capture the time again and take the difference, for example

    //lets say this is where you load the request and you already have your webview set up with a delegate
    
        -(void)loadRequest
        {
           [webView loadRequest:yourRequest];
    
    
           startDate=[NSDate date]
    }
    //this is the delegate call back for UIWebView
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
       NSDate *endDate=[NSDate date];
       double ellapsedSeconds= [endDate timeIntervalSinceDate:startDate];
    }
    

    If you want to do this without the UIWebView you can just use NSURLRequest/NSURLConnection... you can do the following (I will do it synchrnosly you can also do it async)

        NSDate *start=[NSDate date];
        NSURLRequest *r= [[ [NSURLRequest alloc] initWithURL:url] autorelease]; 
        NSData *response=   [NSURLConnection sendSynchronousRequest:r returningResponse:nil error:nil];
       NSDate *end=[NSDate date];
     double ellapsedSeconds= [start timeIntervalSinceDate:end];
    

提交回复
热议问题