Reputation: 29767
can anyone paste some code on how to do a simple http get in cocoa?
Upvotes: 6
Views: 7776
Reputation: 21760
Here you go!
This one grabs an image from a webserver.
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat:@"http://www.somewebsite.com/demo.png"] ];
image = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: url ] ];
or, this one grabs a web page...
NSURL *url = [ NSURL URLWithString:[ NSString stringWithFormat: @"http://www.google.com/search?q=%@", query ] ];
NSURLRequest *request = [ NSURLRequest requestWithURL: url ];
To do it asynchronously, you should check out NSURLConnection.
Upvotes: 9
Reputation: 26859
Take a look at NSURLConnection
. You use it to request a URL, synchronously or (preferably) asynchronously. The full documentation for the URL system is here:
But what you really probably want is:
Apple provides some sample code that should get you started.
Upvotes: 2