问题描述
我搜索了很多,但找不到使用 iOS 测量网页加载时间的方法.在应用程序中,我想显示特定的页面加载时间.. 是否可以使用 iOS sdk 或第三方 sdk?
谢谢
推荐答案
你可以加载一个 URL 请求并使用 NSDate 来查看它花了多长时间...假设你使用 UIWebView 来显示你的页面以便测量加载时间我会捕获请求 URL 的时间,然后在委托方法中 - (void)webViewDidFinishLoad:(UIWebView *)webView 再次捕获时间并获取差异,例如
//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... 您可以执行以下操作(我会同步执行,您也可以异步执行)
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];