Selwyn
Selwyn

Reputation: 1621

FTP file using an async method call in iPhone app

I am new to iPhone app development. I want to have a async method that will be called on successful login and work async while I am navigating to various views in the app.

This method should independently work without affecting the main view methods. This method is performing ftp of the files on the local folder to the server.

Could you please tell me or put some sample code which I can refer to. I want to see both for ftp and async method processes.

Upvotes: 2

Views: 569

Answers (1)

m Jae
m Jae

Reputation: 725

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

Upvotes: 1

Related Questions