iOS Network Request basics

Source: Internet
Author: User

This is about the network request, combined with the actual situation of the company prepared, if there are different views welcome message to discuss together.

iOS completely abandoned nsurlconnection after 9.0 and has now switched to nsurlsession for network requests. In general, network requests are now also using afnetworking. Let's write down my own API usage for AFN and the system.

I. Use of nsurlsession

The network requests on the mobile side are mostly get requests and post requests, the GET and POST request steps are similar, except post is more secure for the transmission of information, the POST request will be set body and the body will be encrypted to ensure the security of the transmitted information.

NSString *urlstring = Base_personal_url; Set the request URL
Nsurl *url = [Nsurl urlwithstring:urlstring];
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; Create request

Parameter stitching
Nsdictionary *bodydic = @{@ "password": Md5password, @ "OS": @ "2", @ "version": Strsysversion, @ "models": Phonemodel};
Nsarray *body = @[bodydic];
Nsdictionary *parameter = @{@ "Head": Head, @ "body": body};
Request. HttpMethod = @ "Post"; Set Request mode
[Request setvalue:@ "Application/json" forhttpheaderfield:@ "Content-type"]; Set the request header, return type
NSLog (@ "parameter = =%@", parameter);

NSData *data = [nsjsonserialization datawithjsonobject:parameter options:nsjsonwritingprettyprinted Error:nil];
Because the body is required to encrypt the data into a string, if not encrypted can be skipped step
NSString *bodystring = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSData *bodydata = [[Base gethttpbodystring:bodystring] datausingencoding:nsutf8stringencoding]; Encrypt the body body
Request. Httpbody = Bodydata; Set Body Body
Make a network request
Nsurlsession *session = [Nsurlsession sharedsession];
Nsurlsessiondatatask *datatask = [Session datataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {
if (!error) {

Because the callback information is also encrypted, the returned data needs to be decrypted.
Convert data to string for decryption
NSString *responsestring = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSString *resault = [Base gethttpstring:responsestring];
Turn decrypted string into data
NSData *resaultdata = [Resault datausingencoding:nsutf8stringencoding];
Parsing with JSON
Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:resaultdata options:nsjsonreadingmutableleaves Error:nil ];

} else {
NSLog (@ "%@", error);
}
}];

Network request start
[Datatask resume];

This is basically the case with the request, as the encryption is determined by the company's internal negotiation.

Two use AFN

Afhttpsessionmanager *sessionmanager = [Afhttpsessionmanager manager];
Sessionmanager.requestserializer = [Afjsonrequestserializer serializer];
Sessionmanager.responseserializer = [Afjsonresponseserializer serializer];

[SessionManager post:urlstring parameters:parameter progress:^ (nsprogress * _nonnull uploadProgress) {

} success:^ (Nsurlsessiondatatask * _nonnull task, id _nullable responseobject) {


NSLog (@ "response = = =%@", responseobject);


} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {
NSLog (@ "data = = =---0=-=0=0-=0=0-=0=%@", error);

}];
The general company will be based on the actual situation of AFN two packages, and not directly call AFN interface, such as encryption, etc.
 

iOS Network Request basics

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.