from my understanding you want to upload something from the iphone to a server in a background thread?
anyway; downloading in a background thread should be quite similar.
first, i suggest you create a method that does the main work for you:
- (void) createRessource {
NSURL *destinationDirURL = [NSURL URLWithString: completePathToTheFileYouWantToUpload];
CFWriteStreamRef writeStreamRef = CFWriteStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) destinationDirURL);
ftpStream = (__bridge_transfer NSOutputStream *) writeStreamRef;
BOOL success = [ftpStream setProperty: yourFTPUser forKey: (id)kCFStreamPropertyFTPUserName];
if (success) {
NSLog(@"\tsuccessfully set the user name");
}
success = [ftpStream setProperty: passwdForYourFTPUser forKey: (id)kCFStreamPropertyFTPPassword];
if (success) {
NSLog(@"\tsuccessfully set the password");
}
ftpStream.delegate = self;
[ftpStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// open stream
[ftpStream open];
}
This method is one third of the job: it will be called in the background.
Invoked from something like this:
- (void) backgroundTask {
NSError *error;
done = FALSE;
/*
only 'prepares' the stream for upload
- doesn't actually upload anything until the runloop of this background thread is run!
*/
[self createRessource];
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
do {
if(![currentRunLoop runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture]]) {
// log error if the runloop invocation failed
error = [[NSError alloc] initWithDomain: @"org.yourDomain.FTPUpload"
code: 23
userInfo: nil];
}
} while (!done && !error);
// close stream, remove from runloop
[ftpStream close];
[ftpStream removeFromRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
if (error) {
// handle error
}
/* if you want to upload more: put the above code in a lopp or upload the next ressource here or something like that */
}
Now you could call
[self performSelectorInBackground: @selector(backgroundTask) withObject: nil];
and a background thread will be created for you, the stream will be scheduled in its runloop and the runloop is configured and started.
Most important is the starting of the runloop in the background thread - without it, the stream implementation will never start working...
mostly taken from here, where i had a similar task to perform:
upload files in background via ftp on iphone