Can't get HTML code AFNetworking 2.0

afnetworking-2, objective-c

Solution

For get html code we will need to build a custom response serializer to decode the NSData response from the web server into a NSString. We will need to subclass AFHTTPResponseSerializer and implement the following method:

- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data 
                          error:(NSError *__autoreleasing *)error
{
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

Problem

I tried to make GET HTTP response. I need to get the html code for the subsequent parsing, but responseObject is nil. ``` AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; [manager GET:@"http://www.example.com/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSError *error; HTMLParser *parser = [[HTMLParser alloc] initWithString:responseObject error:&error]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; ```

Original source