IOS network request base, ios network request
This article is about network requests, which are compiled based on the actual situation of the company. If you have different opinions, please leave a message for discussion.
After 9.0, iOS completely gave up NSURLConnection and now switched to NSURLSession for network requests. Generally, AFNetworking is also used for network requests. Next I will write down my afn and system api usage.
I. Use NSURLSession
Mobile network requests are mainly get requests and post requests. The get and post request steps are similar, except that post is more secure for information transmission. post requests will set the body and encrypt the body, ensure the security of transmitted information.
NSString * urlString = BASE_PERSONAL_URL; // sets the request URL.
NSURL * url = [NSURL URLWithString: urlString];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // create a request
// Parameter concatenation
NSDictionary * bodyDic =@ {@ "password": md5Password, @ "OS": @ "2", @ "version": strSysVersion, @ "models": phoneModel };
NSArray * body = @ [bodyDic];
NSDictionary * parameter =@{@ "head": head, @ "body": body };
Request. HTTPMethod = @ "Post"; // set the request METHOD
[Request setValue: @ "application/json" forHTTPHeaderField: @ "Content-Type"]; // set the request header and return Type
NSLog (@ "parameter ===%@", parameter );
NSData * data = [NSJSONSerialization dataWithJSONObject: parameter options: NSJSONWritingPrettyPrinted error: nil];
// Because the body needs to be encrypted, the data is first converted into a string. If it is not encrypted, skip this step.
NSString * bodyString = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSData * bodyData = [[Base getHTTPBodyString: bodyString] dataUsingEncoding: NSUTF8StringEncoding]; // encrypt the body
Request. HTTPBody = bodyData; // set the body
// Perform network requests
NSURLSession * session = [NSURLSession sharedSession];
NSURLSessionDataTask * dataTask = [session dataTaskWithRequest: request completionHandler: ^ (NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ Nullable error ){
If (! Error ){
// The returned data must be decrypted because the returned information is encrypted.
// Convert data into strings for decryption
NSString * responseString = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSString * resault = [Base getHttpString: responseString];
// Convert the decrypted string to data
NSData * resaultData = [resault dataUsingEncoding: NSUTF8StringEncoding];
// Parse using json
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: resaultData options: NSJSONReadingMutableLeaves error: nil];
} Else {
NSLog (@ "% @", error );
}
}];
// Start network request
[DataTask resume];
The request is basically like this. The encryption method is determined by the company's internal negotiation.
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 );
}];
Generally, companies perform secondary encapsulation of afn based on actual conditions, instead of directly calling the afn interface, such as encryption.