IOS's path to growth-reading data from the network

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.