40media Upload and download data from server with Cocoa Touch?


Upload and download data from server with Cocoa Touch?



How can I upload/download data from a server in Cocoa Touch. Here's what I have so far...

-(void)uploadSchedule:(id)sender { 	NSData *content = [NSData dataWithContentsOfFile:self.dataFilePath]; 	NSString *stuff = [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding];  	NSURL *url = [NSURL URLWithString:@"http://thetis.lunarmania.com"]; 	NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc]initWithURL:url]; 	[urlRequest setHTTPMethod:@"POST"]; 	[urlRequest setHTTPBody:[stuff dataUsingEncoding:NSASCIIStringEncoding]];  	NSLog(@"great success!"); }  -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {     // this method is called when the server has determined that it     // has enough information to create the NSURLResponse      // it can be called multiple times, for example in the case of a     // redirect, so each time we reset the data.     // receivedData is declared as a method instance elsewhere      [receivedData setLength:0]; }  -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {     // append the new data to the receivedData     // receivedData is declared as a method instance elsewhere      [receivedData appendData:data]; }  -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {     // release the connection, and the data object     [connection release];     // receivedData is declared as a method instance elsewhere     [receivedData release];      // inform the user     NSLog(@"Connection failed! Error - %@ %@",           [error localizedDescription],           [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); }  -(void)connectionDidFinishLoading:(NSURLConnection *)connection {     // do something with the data 	UIImage *image = [[UIImage alloc] initWithData:receivedData]; 	[cat setImage:image]; 	[image release];  	// receivedData is declared as a method instance elsewhere     NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);      // release the connection, and the data object     [connection release];     [receivedData release]; }  -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {     if ([challenge previousFailureCount] == 0) {         NSURLCredential *newCredential;         newCredential=[NSURLCredential credentialWithUser:@"ican@moeyo.org"                                                  password:@"icanican"                                               persistence:NSURLCredentialPersistenceNone];         [[challenge sender] useCredential:newCredential                forAuthenticationChallenge:challenge];     } else {         [[challenge sender] cancelAuthenticationChallenge:challenge];         // inform the user that the user name and password         // in the preferences are incorrect         //[self showPreferencesCredentialsAreIncorrectPanel:self];     } } 

I'm so lost...


How do I programatically pull lists/arrays of (itunes urls to) apps in the iphone app store?

1:

iTune-style apps using C#
The code crashes for the reason this you over-release connection. how to remove subviews from scrollview?Review the Cocoa memory management rules.. iPhone PHP integration Aside from that, you'll have to be more specific around what problem you're having with it.. does using try catch block in xcode show error on real device? BTW, the term is “instance variable”, not “method instance”. for- loop in background thread accesses each index more than once — iPhone devAn instance variable is a variable inside of an instance, and has nothing to did with methods.. Update coordinate property of MKAnnotation on parent viewcontroller after Touch End event?
Memory Management Question in Objective C

2:

This has been covered here:. http://stackoverflow.com/questions/787582/nsrequest-encode-url-for-nsrequest-post-body-iphone-objective-c. The accepted answer uses ASIHTTPRequest which is similar to one I've used, and makes it really easy to post/receive from an HTML form. Here's an case (from past stackoverflow).
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:@"http://any Site.com"] autorelease]; [request setPostValue:@"myValue1" forKey:@"myFormField1"]; [request setPostValue:@"myValue2" forKey:@"myFormField2"]; // etc. [request start]; NSError *error = [request error]; if (!error)   NSString *response = [request responseString]; 

3:

And if your file is big, you should better use NSFilehandle, to write data inside didReceiveData, instead of appending..


48 out of 100 based on 18 user ratings 548 reviews