Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void)makeRequest:(NSString*)urlString method:(NSString*)method params:(NSMutableDictionary*)params onComplete:(RequestBlock)callback {
- // create the url
- NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", BASE_URL, urlString]];
- NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
- KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
- NSString *token = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];
- if(!token){
- token = @"NO_TOKEN";
- }
- // set the method (GET/POST/PUT/UPDATE/DELETE)
- [request setHTTPMethod:method];
- [request addValue:[@"Bearer " stringByAppendingString:token] forHTTPHeaderField:@"Authorization"];
- // if we have params pull out the key/value and add to header
- if(params != nil) {
- NSMutableString * body = [[NSMutableString alloc] init];
- for (NSString * key in params.allKeys) {
- NSString * value = [params objectForKey:key];
- [body appendFormat:@"%@=%@&", key, value];
- }
- [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
- }
- // submit the request
- [NSURLConnection sendAsynchronousRequest:request
- queue:[NSOperationQueue mainQueue]
- completionHandler:^(NSURLResponse *response,
- NSData *data, NSError *connectionError) {
- // do we have data?
- if(data && data.length > 0) {
- NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- // if we have a block lets pass it
- if(callback) {
- callback(json);
- }
- HERE IS WHERE I WANT TO TEST IF WE HAVE ERROR JSON or PROPER JSON
- }
- }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement