The solution is a subclass of NSInputStream and implements the open, close, read, hasBytesAvailable methods and does not forget - (NSStreamStatus) streamStatus. The last method is called from http to find out whether we are open, close, or we were completed (NSStreamStatusAtEnd) for sending (there are other statuses, but these are the most important). I use the tmp file as a buffer because I need to send a lot of data, but maybe a data memory buffer might be better. Finally, I am implementing another class where they use their own NSInputStream, here is the code:
NSURL *url = [NSURL URLWithString:@"url"]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; [req setHTTPMethod:@"POST"]; //set headers if you have to do for example: NSString *contentType = [NSString stringWithFormat:@"multipart/form-data"]; [req setValue:contentType forHTTPHeaderField:@"Content-Type"]; //Create your own InputStream instream = [[CustomStream alloc] init]; [req setHTTPBodyStream:instream]; //I remove instream later NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO]; [aConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [aConnection start];
source share