Guest User

Untitled

a guest
May 21st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. +(NSString *) fetchTime
  2. {
  3. NSString *timeString=@"not_set";
  4.  
  5. //Code for URL request here
  6. NSURL *timeURL = [NSURL URLWithString:@"http://www.timeapi.org/utc/now"]
  7.  
  8. return timeString;
  9. }
  10.  
  11. NSURL *timeURL = [NSURL URLWithString:@"http://www.timeapi.org/utc/now"]
  12. NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
  13. cachePolicy:NSURLRequestReloadIgnoringCacheData
  14. timeoutInterval:120];
  15. NSData *urlData;
  16. NSURLResponse *response;
  17. NSError *error;
  18.  
  19. urlData = [NSURLConnection sendSynchronousRequest:urlRequest
  20. returningResponse:&response
  21. error:&error];
  22.  
  23. NSString *string = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
  24.  
  25. -(void) fetchTimeFromServerWithCompletionHandler:(void(^)(id)) onComplete {
  26. NSURLRequest *timeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.timeapi.org/utc/now"]];
  27. [NSURLConnection sendAsynchronousRequest:timeRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error) {
  28. // Do something usefull with Data.
  29. // If expected object is a String, alloc init a String with received Data
  30. NSString *time = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  31. onComplete(time); // This will return back the time string.
  32. }];
  33. }
  34.  
  35. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  36. [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
  37. NSLog(@"JSON: %@", responseObject);
  38. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  39. NSLog(@"Error: %@", error);
  40. }];
Add Comment
Please, Sign In to add comment