如何在 iPhone 中使用 ASIHTTP 请求暂停和恢复下载文件

发布于 2024-12-06 01:41:58 字数 67 浏览 1 评论 0原文

我正在使用 ASIHTTP 请求示例源代码来下载 url。 我的问题是如何暂停和恢复下载文件。

请帮忙。

I m using ASIHTTP Request sample source code for downloading urls.
My question is that how to pause and resume the downloading files.

Please Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能怎样 2024-12-13 01:41:58

弃用通知

ASIHTTP 在 2011 年左右被弃用,如果您现在想要网络连接,您应该查找 AFNetworking for ObjCAlamofire for Swift,或者坚持使用原生支持:

对于原生支持,在iOS 7中Apple添加了NSURLSession,可以启动下载任务(NSURLSessionDownloadTask),并且这些任务可以取消和重新启动,查看方法来自 NSURLSession 的 downloadTaskWithResumeData(_:),有一个很好的解释。

不再需要 ASIHTTP,如果您必须保持旧应用程序运行,那么祝您好运。

原始答案

发布于 2011

ASI 本身可以使用 [myASIRequest setAllowResumeForFileDownloads:YES]; 从文件恢复下载,请查看 如何使用 例子。

编辑:好吧,没有简单的方法来暂停下载([myRequest Pause] 是理想的选择)。阅读并尝试了一段时间后,发现取消请求并重新发送是唯一的方法。

例如,这是我制作的一个示例类:

@interface TestViewController : UIViewController <ASIHTTPRequestDelegate>
{
    ASIHTTPRequest *requestImage;
    UIImageView *imageViewDownload;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageViewDownload;

- (IBAction)didPressPauseButton:(id)sender;
- (IBAction)didPressResumeButton:(id)sender;

开始(或重新启动)下载:

- (IBAction)didPressResumeButton:(id)sender
{
    if (requestImage)
        return;
    
    NSLog(@"Resumed");
    
    // Request
    requestImage = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.myheadhealth.com/_IMAGES/cheese.jpg"]];
    
    // Using a temporary destination path just for show. 
    // Better pick a suitable path for your download.
    NSString *destinationDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"cheese.jpg"];
    NSString *temporaryDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"cheese.jpg-part"];
    
    requestImage.downloadDestinationPath = destinationDownloadPath;
    requestImage.temporaryFileDownloadPath = temporaryDownloadPath;
    requestImage.allowResumeForFileDownloads = YES;
    requestImage.delegate = self;
    [requestImage startAsynchronous];
}

在上面的示例中,我正在加载我找到的奶酪的随机图像,并存储临时下载和完整下载。

取消请求将使临时文件的内容保持不变,事实上,我制作了一个 UIImageView,当按“取消”时,我可以看到下载文件的一部分。如果你想尝试一下:

- (void)didPressPauseButton:(id)sender
{
    if (requestImage)
    {
        // An imageView I made to check out the contents of the temporary file.
        imageViewDownload.image = [UIImage imageWithContentsOfFile:requestImage.temporaryFileDownloadPath];
        
        [requestImage clearDelegatesAndCancel];
        requestImage = nil;
        
        NSLog(@"Canceled");
    }
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    imageViewDownload.image = [UIImage imageWithContentsOfFile:request.downloadDestinationPath];
    
    requestImage = nil;
}

Deprecation Notice

ASIHTTP was deprecated around 2011, if you want network connectivity now, you should look up AFNetworking for ObjC or Alamofire for Swift, or stick to the native support:

For native support, in iOS 7 Apple added the NSURLSession, which can start download tasks (NSURLSessionDownloadTask), and these can be cancelled and restarted, check the method downloadTaskWithResumeData(_:) from NSURLSession, that has a very good explanation.

ASIHTTP is no longer needed, if you gotta are keeping a legacy app running, well, good luck.

Original Answer

Posted on 2011

ASI itself can resume a download from a file using [myASIRequest setAllowResumeForFileDownloads:YES];, check out the How-to-Use for an example.

Edit: Ok, there's no easy way to pause a download (a [myRequest pause] would be ideal). After reading and trying for a while, turns out that cancelling the request and resending it again is the only way.

For example, here's an example class I made:

@interface TestViewController : UIViewController <ASIHTTPRequestDelegate>
{
    ASIHTTPRequest *requestImage;
    UIImageView *imageViewDownload;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageViewDownload;

- (IBAction)didPressPauseButton:(id)sender;
- (IBAction)didPressResumeButton:(id)sender;

To start (or restart) the download:

- (IBAction)didPressResumeButton:(id)sender
{
    if (requestImage)
        return;
    
    NSLog(@"Resumed");
    
    // Request
    requestImage = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.myheadhealth.com/_IMAGES/cheese.jpg"]];
    
    // Using a temporary destination path just for show. 
    // Better pick a suitable path for your download.
    NSString *destinationDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"cheese.jpg"];
    NSString *temporaryDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"cheese.jpg-part"];
    
    requestImage.downloadDestinationPath = destinationDownloadPath;
    requestImage.temporaryFileDownloadPath = temporaryDownloadPath;
    requestImage.allowResumeForFileDownloads = YES;
    requestImage.delegate = self;
    [requestImage startAsynchronous];
}

In the exmaple above, I'm loading a random image of cheese I found, and storing a temporary download, and a full download.

Canceling the request will keep the contents of the temporary file intact, in fact, I made an UIImageView, and when pressing "Cancel" I could see a part of the downloaded file. If you want to try it out:

- (void)didPressPauseButton:(id)sender
{
    if (requestImage)
    {
        // An imageView I made to check out the contents of the temporary file.
        imageViewDownload.image = [UIImage imageWithContentsOfFile:requestImage.temporaryFileDownloadPath];
        
        [requestImage clearDelegatesAndCancel];
        requestImage = nil;
        
        NSLog(@"Canceled");
    }
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    imageViewDownload.image = [UIImage imageWithContentsOfFile:request.downloadDestinationPath];
    
    requestImage = nil;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文