Logic:
First create an nsurl address, send a request, and then call
-(Void) connection :( nsurlconnection *) connection didreceivedata :( nsmutabledata
*) Data
Store the downloaded data to the nsdate object. After the download is complete, run
-(Void) connectiondidfinishloading :( nsurlconnection *) connection method,
If an error occurs during connection:
-(Void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) Error
Implementation:
// Define an NSDate object NSMutableData * buffer; /* = ------------------ = */-(void) viewDidLoad {// call the down method [self Down]; // initialize buffer = [[NSMutableData alloc] init]; [super viewDidLoad];}-(void) down {// create url NSURL * url = [NSURL URLWithString: @ "http: // 192.168.67.3: 8080/movie/3.mp4"]; // http-based connection request NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url]; // create a connection session NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate: self]; // release [connection release]; [request release];} // provide an NSData, encapsulate the byte block that you just received from the connection. // when you download and add data, a data callback is generated (it is a part of the download) until all the data is downloaded.-(void) connection :( NSURLConnection *) connection didReceiveData :( NSMutableData *) data {// buffer is a modifiable data, which adds the data downloaded from the network to [buffer appendData: data];} // After the http-based session is completed, it will be executed (that is, the download is completed)-(void) connectionDidFinishLoading :( NSURLConnection *) connection {// create a file, specify the path, name, and content [[NSFileManager defaultManager] createFileAtPath: @ "/tmp/movie.mp4" contents: buffer attributes: nil];} // It is called when the network connection fails or an exception occurs. An NSError is provided to explain the cause of the failure-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {// load a warning box: UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle: [error localizedDescription] message: [error localizedFailureReason] delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [errorAlert show]; [errorAlert release];}