简体   繁体   中英

How to read a file from a website using objective-c & xcode

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

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM