How to Pause and Resume Downloading Files with ASIHTTP Request in iPhone

前端 未结 1 874
梦谈多话
梦谈多话 2021-02-06 19:46

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.

1条回答
  •  余生分开走
    2021-02-06 20:22

    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 
    {
        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;
    }
    

    0 讨论(0)
提交回复
热议问题