How to make NSURLConnection file download work?

后端 未结 3 2005
醉梦人生
醉梦人生 2021-01-25 04:12

I have a ViewController declared as:

@interface DownloadViewController : UIViewController 
           
<         


        
相关标签:
3条回答
  • 2021-01-25 04:52

    If you're having problems, you could consider using the well regarded ASIHTTPRequest library to manage your download. It takes care of everything for you.

    For example, just 2 lines will do it.

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadDestinationPath:fullPathOfWhereToStoreFile];
    
    0 讨论(0)
  • 2021-01-25 04:52

    Use "NSURLConnection asynchronously" search for the term and you'll find source. Or just NSURLConnection.

    For example:

    NSURLConnection NSURLRequest proxy for asynchronous web service calls

    Using NSURLConnection from apple with example code

    Objective-C Programming Tutorial – Creating A Twitter Client Part 1

    0 讨论(0)
  • 2021-01-25 05:02

    Using a UIViewController instead of an NSObject should not be your problem here ! I'm using a NSURLConnection in an UIViewController with no issue ! Here is a part of my code (not sure it will compile as it is) :

    //
    //  MyViewController.h
    //
    
    #import <Foundation/Foundation.h>
    
    @interface MyViewController : UIViewController {
        @protected
        NSMutableURLRequest* req;
        NSMutableData* _responseData;
        NSURLConnection* nzbConnection;
    }
    
    - (void)loadFileAtURL:(NSURL *)url;
    
    @end
    

    -

    //
    //  MyViewController.m
    //
    
    #import "MyViewController.h"
    
    @implementation MyViewController
    
    - (void)loadView {  
    // create your view here
    }
    
    - (void) dealloc {
        [_responseData release];
    
        [super dealloc];
    }
    
    #pragma mark -
    
    - (void)loadFileAtURL:(NSURL *)url {
        // allocate data buffer
        _responseData = [[NSMutableData alloc] init];
    
        // create URLRequest
        req = [[NSMutableURLRequest alloc] init];
        [req setURL:_urlToHandle];
    
        nzbConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
        [req release];
        req = nil;
    }
    
    
    #pragma mark -
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        // Append data in the reception buffer
        if (connection == nzbConnection)
            [_responseData appendData:data];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        if (connection == nzbConnection) {
            [nzbConnection release];
            nzbConnection = nil;
    
            // Print received data
            NSLog(@"%@",_responseData);
    
            [_responseData release];
        }
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        // Something went wrong ...
        if (connection == nzbConnection) {
            [nzbConnection release];
            [_responseData release];
        }
    }
    
    @end
    

    If you plan to download large files, consider storing the received packets in a file instead of storing it in memory !

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