Skip to content

Instantly share code, notes, and snippets.

@arkilis
Last active March 18, 2020 13:46
Show Gist options
  • Save arkilis/307c10fe5e73eb691ffa to your computer and use it in GitHub Desktop.
Save arkilis/307c10fe5e73eb691ffa to your computer and use it in GitHub Desktop.
Afnetworking 3 example and usage
// 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