Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // NetworkConnection.m
- // Elearning
- //
- #import "NetworkConnection.h"
- @implementation NetworkConnection
- + (void) responseWithUrl:(NSString*)url
- method:(NSString*)method
- params:(NSString*)params
- resultRequest:(ResultRequest)complete
- {
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
- if ([method isEqualToString:@"GET"]) {
- // GET method
- [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",url,params]]];
- request.HTTPMethod = @"GET";
- } else {
- // POST, DELETE, PATCH method
- [request setURL:[NSURL URLWithString:url]];
- NSString * post = params;
- NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
- NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
- request.HTTPMethod = method;
- [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
- [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
- request.HTTPBody = postData;
- }
- NSURLSession *session = [NSURLSession sharedSession];
- [[session dataTaskWithRequest:request
- completionHandler:^(NSData *data,
- NSURLResponse *response,
- NSError *error) {
- NSError *errors;
- NSDictionary *dic;
- if (data) {
- dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errors];
- }
- if (complete) complete(dic,error);
- }] resume];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement