我搜索了很多,但找不到用 iOS 测量网页加载时间的方法。
在应用程序中,我想显示特定的页面加载时间。iOS sdk 或第三方 sdk 是否可以?
Thanks
您可以加载 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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)