Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +(NSString *) fetchTime
- {
- NSString *timeString=@"not_set";
- //Code for URL request here
- NSURL *timeURL = [NSURL URLWithString:@"http://www.timeapi.org/utc/now"]
- return timeString;
- }
- NSURL *timeURL = [NSURL URLWithString:@"http://www.timeapi.org/utc/now"]
- NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
- cachePolicy:NSURLRequestReloadIgnoringCacheData
- timeoutInterval:120];
- NSData *urlData;
- NSURLResponse *response;
- NSError *error;
- urlData = [NSURLConnection sendSynchronousRequest:urlRequest
- returningResponse:&response
- error:&error];
- NSString *string = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
- -(void) fetchTimeFromServerWithCompletionHandler:(void(^)(id)) onComplete {
- NSURLRequest *timeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.timeapi.org/utc/now"]];
- [NSURLConnection sendAsynchronousRequest:timeRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error) {
- // Do something usefull with Data.
- // If expected object is a String, alloc init a String with received Data
- NSString *time = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- onComplete(time); // This will return back the time string.
- }];
- }
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"JSON: %@", responseObject);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"Error: %@", error);
- }];
Add Comment
Please, Sign In to add comment