Get data from the network:
1. access to data from the network, using the following approach will lead to a blocking phenomenon, the main line is responsible for the interaction of the UI ( user input instructions or data, the system gives a feedback )
Will further let the UI stop interacting
1) First set up a location for the pictures we will download
UIImage *image = [UIImage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:@ " address "]];
Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (50, 100, 100, 80)];
Imageview.image = image;
2). load to view
Self.view Addsubview:imageview];
Several steps of network download
1. Start the request
2. implement the Proxy method to initialize the data
3. Start accepting Data
4. Processing of accepted data
5. handling of data when acceptance fails
2. We should use this method to request the network data, the network asynchronous load
1). Asynchronous loading to follow a proxy nsurlconnectiondatadelegate
2). An initialization of a defined global variable to hold the data
_data = [[Nsmutabledata alloc]init];
Nsurl *url = [Nsurl urlwithstring:string];
3). Send a request like a network, request data
Nsurlrequest *req = [Nsurlrequest Requestwithurl:url];
4). A class that makes requests for a network, specifically to make a class of network connections
Nsurlconnection *conn = [[Nsurlconnection alloc]initwithrequest:req delegate:self Startimmediately:no];
5). Start request
[Conn start];
6 ). Implement proxy method , start receiving data, initialize the data, each download is initialized to 0, ensure that the length of the download is set to 0. Empty the original content
-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{
_data.length = 0;
}
7). Start receiving stitching data , that is, append data. Similar to our previous practice of appending strings
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{
[_data Appenddata:data];
}
8). Processing data that is downloaded to completion
-(void) connectiondidfinishloading: (nsurlconnection *) connection{
UIImage *image = [UIImage imagewithdata:_data];
Uiimageview *vie = [[Uiimageview alloc]initwithframe:cgrectmake (50, 100, 150, 100)];
Vie.image = image;
[Self.view Addsubview:vie];
}
9). Error handling , error handling with data errors, and network address errors
-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{
NSLog (@ " error ");
}
3. Request network data in a way that uses a third-party library
Step,1). Pour into a third-party library,
2). Add a -info-objc-arc to the end of the . M file in the library to solve the arc problem ( as long as you consider the library of arc cases, if not you don't need it )
This saves a lot of code
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS learning (getting data from the Web)