使用cocoa touch测量iPhone下载速度的最佳方法 [英] Best way to measure download speed on iPhone using cocoa touch

查看:108
本文介绍了使用cocoa touch测量iPhone下载速度的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,其中我想要提供的功能之一是测量连接的下载速度。为此,我使用NSURLConnection开始下载大文件,并在一段时间后取消下载并进行计算(数据下载/时间已过)。
虽然像speedtest.net这样的其他应用程序每次都会给出恒定的速度,但我的波动幅度大约为2-3 Mbps。

Im making an app where one of the features I want to offers is to measure de download speed of the connection. To get this I`m using NSURLConnection to start the download of a large file, and after some time cancel the download and make the calculation (Data downloaded / time elapsed). While other apps like speedtest.net give a constant speed every time, mine fluctuates 2-3 Mbps more or less.

基本上我正在做的是,当方法连接:didReceiveResponse:被调用时启动计时器。 500调用方法连接后:didReceiveData:我取消下载,停止计时器并计算速度。

Basically what I`m doing is, start the timer when the method connection:didReceiveResponse: is called. After 500 call of the method connection:didReceiveData: I cancel the download, stop the timer and calculate the speed.

以下是代码:

- (IBAction)startSpeedTest:(id)sender 
{
    limit = 0;
    NSURLRequest *testRequest = [NSURLRequest requestWithURL:self.selectedServer  cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

    NSURLConnection *testConnection = [NSURLConnection connectionWithRequest:testRequest delegate:self];
    if(testConnection) {
        self.downloadData = [[NSMutableData alloc] init];
    } else {
        NSLog(@"Failled to connect");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.startTime = [NSDate date];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.downloadData appendData:data];
    if (limit++ == 500) {
        [self.connection cancel];
        NSDate *stop = [NSDate date];
        [self calculateSpeedWithTime:[stop timeIntervalSinceDate:self.startTime]];
        self.connection = nil;
        self.downloadData = nil;
    }
}

我想知道是否有更好的方法这样做。一个更好的算法,或者更好的类。

I would like to know if there is a better way of doing this. A better algorithm, or a better class to use.

谢谢。

推荐答案

一旦开始下载,捕获当前系统时间并将其存储为 startTime 。然后,您需要做的就是在下载过程中的任何时刻计算数据传输速度。只需再次查看系统时间并将其用作 currentTime 来计算到目前为止所花费的总时间。

As soon as you start the download, capture the current system time and store it as the startTime. Then, all you need to do is calculate data transfer speed at any point during the download. Just look at the system time again and use it as the currentTime to calculate the total time spent so far.


downloadSpeed = bytesTransferred /(currentTime - startTime)

downloadSpeed = bytesTransferred / (currentTime - startTime)

喜欢这样:

static NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];    
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
double downloadSpeed = totalBytesWritten / (currentTime - startTime);

您可以在 NSURLConnectionDownloadDelegate 中使用此方法:

You can use this method from NSURLConnectionDownloadDelegate:

- (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes;

这篇关于使用cocoa touch测量iPhone下载速度的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆