Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

AFNetworking 2.0 Tutorial

Learn how to easily get and post data from a web service in iOS in this AFNetworking 2.0 tutorial.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2516-afnetworking-2-0-tutorial

Very Nice tutorial…
Very well explained…

Can this tutorial be updated to Swift and iOS 9.3?

Unfortunately, this tutorial will not likely be updated.

If you’re already using Swift, I highly recommend you switch over to Alamofire instead. This is an excellent library by the same author of AFNetworking, Mattt Thompson.

Along with using AlamofireImage, it’s functionality is on par, perhaps even better than, AFNetworking.

iv been away from coding for som time, and i made ale my apps with web api after then tutorial How To Create an App Like Instagram With a Web Service Backend – Part 1/2 2/2 and my question if can the function below still be used in AFNetworking 2.0 / 3.0 ?

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock {
NSData* uploadFile = nil;
if ([params objectForKey:@"file"]) {
	uploadFile = (NSData*)[params objectForKey:@"file"];
	[params removeObjectForKey:@"file"];
}

NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST" path:kAPIPath  parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
	if (uploadFile) {
		[formData appendPartWithFileData:uploadFile name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
	}
}];
AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    
}];
[operation start];
}

Hi skov, I’ve had to adapt the code to use with AFNetworking 3.0 in the method jsonTapped:

  • (IBAction)jsonTapped:(id)sender
    {
    // 1
    NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
    NSURL *url = [NSURL URLWithString:string];

    // 2
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    // 3
    self.weather = (NSDictionary *)responseObject;
    self.title = @“JSON Retrieved”;
    [self.tableView reloadData];

    } failure:^(NSURLSessionTask *operation, NSError *error) {
    // 4
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@“Error Retrieving Weather”
    message:[error localizedDescription]
    delegate:nil
    cancelButtonTitle:@“Ok”
    otherButtonTitles:nil];
    [alertView show];
    }];
    }

I’ll continue doing this great tutorial.

For more information about what has changed between both versions: here

Hey , thanks for your replay, but you updated the AFNetworking 2.0 tutorial instead og my post :smiley:

http://www.raywenderlich.com/demos/weather_sample/weather.php?format=xml

URL is not working

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]