【发布时间】:2011-08-26 21:37:51
【问题描述】:
我搜索了很多,但找不到使用 iOS 测量网页加载时间的方法。 在应用程序中,我想显示特定的页面加载时间.. 是否可以使用 iOS sdk 或第三方 sdk?
谢谢
【问题讨论】:
标签: ios
我搜索了很多,但找不到使用 iOS 测量网页加载时间的方法。 在应用程序中,我想显示特定的页面加载时间.. 是否可以使用 iOS sdk 或第三方 sdk?
谢谢
【问题讨论】:
标签: ios
您可以加载一个 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];
【讨论】:
假设您正在使用UIWebView,您可以设置它的delegate 并在委托的-webViewDidFinishLoad: 方法中记下时间。这次和之前调用 webview 的 -loadRequest: 方法之间的差异应该会给你加载时间。
【讨论】:
NSURLRequest 是同步获取数据的。我宁愿建议使用NSURLConnection。