Showing posts with label AFNetworking. Show all posts
Showing posts with label AFNetworking. Show all posts

Friday, 3 February 2017

AFNetworking - How to handle HTTP status error codes and messages in failure block?

failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
 NSLog(@"Failure: %@", error); 
 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;  
 NSLog(@"%zd", httpResponse.statusCode); 
 id errorJson = [NSJSONSerialization  JSONObjectWithData:error.userInfo[AFNetworkingOperationFailingURLResponseDataError  Key] options:0 error:nil];
 NSDictionary *errorJsonDict = (NSDictionary *)errorJson; 
 if (!errorJsonDict)    
    return;
 if ([errorJsonDict isKindOfClass:[NSDictionary class]] == NO) 
     NSAssert(NO, @"Expected an Dictionary, got %@",NSStringFromClass([errorJsonDict  class])); 

  NSLog(@"%@",errorJsonDict.description); 
}

Saturday, 20 February 2016

Geocoding Google map in iOS

#define GEOCODING_URI @"https://maps.googleapis.com/maps/api/geocode/json?key=&language=en-US&sensor=true&address=" // TODO: add DEV_KEY for Geocoding
NSString *actionURI = [NSString stringWithFormat:@"%@%@",GEOCODING_URI, self.geocodeTextField.text];
NSLog(@"ACTION URL: %@",actionURI);
NSString *encodedURI = [actionURI stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManager manager];
[manager GET:encodedURI parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject); 
NSDictionary *jsonDict = (NSDictionary *)responseObject;
if (!jsonDict)
   return;
if ([jsonDict isKindOfClass:[NSDictionary class]] == NO)
   NSAssert(NO, @"Expected a dictionary, got %@", NSStringFromClass([jsonDict class]));     
if ([jsonDict[@"status"] isEqualToString:@"OK"]) {
    NSArray *locGeometryResult=[[jsonDict valueForKeyPath:@"results.geometry"] objectAtIndex:0];
    NSDictionary *locationDict = [locGeometryResult valueForKey:@"location"];
    NSLog(@"JSON: %@", locationDict.description);
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([locationDict[@"lat"] doubleValue], [locationDict[@"lng"] doubleValue]);
    GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:coordinate zoom:17];
    [self.mapView animateWithCameraUpdate:updatedCamera];
} else {
        NSLog(@"No location found.");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@"); 
}];

Wednesday, 4 December 2013

Setting UITableViewCell imageview via AFNetworking

#import "UIImageView+AFNetworking.h" Download
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"YOUR_URL"]];
[cell.imageView setImageWithURLRequest:imageRequest placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
        NSLog(@"success");
        cell.imageView.image = image;
        cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
        cell.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell setNeedsLayout];// To update the cell {if not using this, your image is not showing over cell.}
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
        NSLog(@"Failure");}

Thursday, 10 October 2013

Image URL load via AFNetworking | Wait Indicator placed while loading URL on UIImageView | Exact width/height of image width/height

If you want to maintain the aspect ratio of an image, use contentMode=UIViewContentModeScaleAspectFit
contentMode=UIViewContentModeScaleAspectFill (if you want to use full layout) and masking with width or height, Make sure that use Bitwise operator used i.e UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleHeight
* masking is working only upon if content mode is set.
* setImageWithURLRequest, use this function: 
#import "UIImageView+AFNetworking.h"
UIImageView *imgView = [[UIImageView alloc] init];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.clipsToBound = YES;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIActivityIndicatorView *loadingIndicator =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
loadingIndicator.center=CGPointMake(150,75);
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"YOUR_URL"]];
[imgView addSubview:loadingIndicator];
[self.view addSubview:imgView];
[loadingIndicator setHidden:NO];
[loadingIndicator startAnimating];
[imgView setImageWithURLRequest :imageRequest placeholderImage:nil success:^(NSURLRequest *request,NSHTTPURLResponse *response,UIImage *image){
        [loadingIndicator setHidden:YES];
        [loadingIndicator stopAnimating];
        imgView.image = image;
       //Here you set your frame accordingly
        CGSize size = img.size;
        imgView.frame = CGRectMake(xOffset,yOffset,size.width,size.height);
}failure:^(NSURLRequest *request,NSHTTPURLResponse *response, NSError *error){
        [loadingIndicator setHidden:YES];
        [loadingIndicator stopAnimating];
}];