How to do http get in cocoa on iPhone

can someone paste some code on how to make simple http access in cocoa?

+6
get cocoa
source share
2 answers

Here you go!

This file captures an image from a web server.

NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat:@"http://www.somewebsite.com/demo.png"] ]; image = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: url ] ]; 

or, this one captures a web page ...

  NSURL *url = [ NSURL URLWithString:[ NSString stringWithFormat: @"http://www.google.com/search?q=%@", query ] ]; NSURLRequest *request = [ NSURLRequest requestWithURL: url ]; 

To do this asynchronously, you must check the NSURLConnection .

+9
source

Take a look at NSURLConnection . You use it to request a URL, synchronously or (preferably) asynchronously. Full documentation for the URL system:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html

But you really want to:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE

Apple provides some sample code that should run you.

+2
source

All Articles