Last active
March 18, 2020 13:46
-
-
Save arkilis/307c10fe5e73eb691ffa to your computer and use it in GitHub Desktop.
Afnetworking 3 example and usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GET | |
NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"]; | |
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; | |
[manager GET:URL.absoluteString | |
parameters:nil | |
progress:nil | |
success:^(NSURLSessionTask *task, id responseObject) { | |
NSLog(@"JSON: %@", responseObject); | |
} | |
failure:^(NSURLSessionTask *operation, NSError *error) { | |
NSLog(@"Error: %@", error); | |
} | |
]; | |
// POST | |
NSURL *URL = [NSURL URLWithString:@"http://yoururl"]; | |
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; | |
[manager POST:URL.absoluteString | |
parameters:@{ | |
@"key1":@"val1", | |
@"key2":@"val2" | |
} | |
progress:nil | |
success:^(NSURLSessionTask *task, id responseObject) { | |
//NSLog(@"JSON: %@", responseObject); | |
} | |
failure:^(NSURLSessionTask *operation, NSError *error) { | |
NSLog(@"Error: %@", error); | |
} | |
]; | |
}; | |
// For POST you can set up Timeout | |
// POST | |
NSURL *URL = [NSURL URLWithString:@"http://yoururl"]; | |
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; | |
[manager.requestSerializer setTimeoutInterval:300]; // set up time out as 5 mins | |
[manager POST:URL.absoluteString | |
parameters:@{ | |
@"key1":@"val1", | |
@"key2":@"val2" | |
} | |
progress:nil | |
success:^(NSURLSessionTask *task, id responseObject) { | |
//NSLog(@"JSON: %@", responseObject); | |
} | |
failure:^(NSURLSessionTask *operation, NSError *error) { | |
NSLog(@"Error: %@", error); | |
} | |
]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment