iOS测量网页加载时间 [英] iOS measuring web page loading time
问题描述
我搜索了很多但是找不到用iOS测量网页加载时间的方法。
在应用程序中,我想显示某些页面加载时间..是否可以使用iOS sdk或第三方sdk?
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 third party sdk?
谢谢
推荐答案
您可以加载URL请求并使用NSDate查看花了多长时间...让我们假设您使用UIWebView来显示您的页面测量加载时间我将捕获请求URL的时间,然后在委托方法中 - (void)webViewDidFinishLoad:(UIWebView *)webView再次捕获时间并获取差异,例如
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];
}
如果你想在没有UIWebView的情况下这样做,你可以使用NSURLRequest / NSURLConnection ...你可以做以下事情(我会同步做你也可以做异步)
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];
这篇关于iOS测量网页加载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!