miércoles, 2 de marzo de 2016

iOS: REST request with sample API

Podfile

Add "pod "AFNetworking", "~> 2.0"" to your podfile

Info.plist

Add row named "App Transport Security Settings" and add object "Allow Arbitrary Loads" with value "YES" (then change the security level accordingly)

Code
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    netmanager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.cheapshark.com/api/1.0/"]];
    reqSerializer = [AFJSONRequestSerializer serializer];
    [reqSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [netmanager setRequestSerializer:reqSerializer];
    netmanager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"application/json", nil];
    
    NSMutableIndexSet* codes = [NSMutableIndexSet indexSetWithIndexesInRange: NSMakeRange(200, 100)];
    [codes addIndex: 400];
    [codes addIndex: 401];
    [codes addIndex: 404];
    [codes addIndex: 408];
    [codes addIndex: 409];
    [codes addIndex: 500];
    netmanager.responseSerializer.acceptableStatusCodes = codes;
    
    [netmanager GET:@"deals?storeID=6&desc=0&title=civilization%20V&pageSize=2" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSLog(@"Success");
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"Failure");        
    }];
}

No hay comentarios:

Publicar un comentario