View Single Post
Old 10-10-2008, 07:01 PM   #2 (permalink)
Brad Larson
Junior Member
Default Avatar
 
Join Date: Oct 2008
Location: Madison, WI
Posts: 5
Default

You'll need to use an NSURLConnection to connect to your server.

For example:

Code:

You must Login or Register to view and contribute code! This is done to increase participation in helping one another out, if you have been helped please pass on the favor.

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/test.html"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	
if (theConnection) 
{
  // Do a little initialization
} 
else 
{
  // inform the user that the connection could not be made
}
This will create an asynchronous connection that will go out, connect to that URL, and start downloading data from it.

You'll then need to implement the following delegate methods in the current class:

Code:

You must Login or Register to view and contribute code! This is done to increase participation in helping one another out, if you have been helped please pass on the favor.

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
These methods will be triggered at various points in the downloading process by the background thread that is actually doing the downloading. If the concepts of delegate methods and so forth are confusing, I'd recommend starting with an introductory guide to Cocoa, such as Aaron Hillegass's excellent "Cocoa Programming for Mac OS X" book. Also, as I mentioned in a previous post, the source code to my Molecules application is available. It contains three different cases of communicating to a server to search for, and download, specific files.
Brad Larson is offline   Reply With Quote