使用Cocoa Touch从服务器上传和下载数据?
如何从Cocoa Touch的服务器上传/下载数据? 这是我到目前为止…
-(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]; } }
我好迷失
代码崩溃,因为你过度释放connection
。 查看cocoa内存pipe理规则 。
除此之外,你必须更具体地说明你的问题。
顺便说一句,这个术语是“实例variables”,而不是“方法实例”。 实例variables是一个实例内部的variables,与方法无关。
这已经被覆盖在这里:
NSURLRequest – 为NSURLRequest POST Body(iPhone objective-C)编码url
接受的答案使用ASIHTTPRequest ,它与我使用的ASIHTTPRequest类似,并且使得从HTML表单发布/获取非常容易。 这里有一个例子(从过去的stackoverflow)
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:@"http://someSite.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];
如果你的文件很大,你最好使用NSFilehandle
,在NSFilehandle
中写数据,而不是追加。