3

I am trying to write a code that will grab some information provided by my server (remote), for example request a url that will return data that will be represented in the application after parsing it.

I've been trying since 2 days now I googled it i found some incomplete solution but nothing really worked out for me

I am really noob in Xcode and Objective-C

Thanks

bhappy
  • 62
  • 3
  • 17

1 Answers1

9

Click for the URL-Loading documentation provided by Apple. Especially Using NSURLConnection looks interesting for you.

Edit: Another very good and easy-to-use Framework for this task is ASIHTTP: Click

The easiest way:

- (void)grabURL
{
  NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  [request startSynchronous];
  NSError *error = [request error];
  if (!error) {
    NSString *response = [request responseString];
  }
}

Asynchronous loading is only slightly more complex:

- (void)grabURLInBackground
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}
jsadfeew
  • 2,247
  • 1
  • 18
  • 25
  • 1
    @bhappy Do it the first way since you are a noob. It'll work for just about whatever you want to do. – Matt S. Dec 25 '10 at 23:39
  • ahaha +1 for both synchronous and asynchronous examples but mainly for the alleseeing-i url lol!! ;) awesome! – Pavan Sep 27 '13 at 01:28