Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. -(void)makeRequest:(NSString*)urlString method:(NSString*)method params:(NSMutableDictionary*)params onComplete:(RequestBlock)callback {
  2.  
  3. // create the url
  4. NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", BASE_URL, urlString]];
  5. NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
  6.  
  7.  
  8. KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
  9.  
  10. NSString *token = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];
  11.  
  12. if(!token){
  13.  
  14. token = @"NO_TOKEN";
  15.  
  16.  
  17.  
  18. }
  19.  
  20.  
  21. // set the method (GET/POST/PUT/UPDATE/DELETE)
  22. [request setHTTPMethod:method];
  23. [request addValue:[@"Bearer " stringByAppendingString:token] forHTTPHeaderField:@"Authorization"];
  24.  
  25.  
  26. // if we have params pull out the key/value and add to header
  27. if(params != nil) {
  28. NSMutableString * body = [[NSMutableString alloc] init];
  29. for (NSString * key in params.allKeys) {
  30. NSString * value = [params objectForKey:key];
  31. [body appendFormat:@"%@=%@&", key, value];
  32. }
  33. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  34. }
  35.  
  36. // submit the request
  37. [NSURLConnection sendAsynchronousRequest:request
  38. queue:[NSOperationQueue mainQueue]
  39. completionHandler:^(NSURLResponse *response,
  40. NSData *data, NSError *connectionError) {
  41.  
  42. // do we have data?
  43. if(data && data.length > 0) {
  44.  
  45. NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  46.  
  47. // if we have a block lets pass it
  48. if(callback) {
  49. callback(json);
  50.  
  51.  
  52. }
  53.  
  54. HERE IS WHERE I WANT TO TEST IF WE HAVE ERROR JSON or PROPER JSON
  55.  
  56.  
  57. }
  58.  
  59.  
  60. }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement