Making an FTP Connection in iPhone Application

By: Jonathan Zdziarski Emailed: 1698 times Printed: 2196 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The CFFTP API is similar to the CFHTTP API, and relies on read streams to transmit FTP data. To create an FTP request, use the CFReadStreamCreateWithFTPURL function, as shown below. This will create the initial read stream to the FTP server:

CFStringRef url = CFSTR("ftp://ftp.somedomain.com/file.txt");
CFURLRef requestURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL);

CFReadStreamRef readStream = CFReadStreamCreateWithFTPURL(
    kCFAllocatorDefault, requestURL);

Once you have created the read stream, you can tie a callback function to it so that a read function will be called when data is ready:

CFReadStreamSetClient(
    readStream,
    kCFtreamEventHasBytesAvailable,
    readCallBack,
    NULL);

The callback function will be called whenever data is available, and will be able to read the data off the stream:

void readCallBack(
    CFReadStreamRef stream,
    CFStreamEventType eventType,
    void *clientCallBackInfo)
{
    char buffer[1024];
    CFIndex bytes_recvd;
    int recv_len = 0;

    while(recv_len < total_size && recv_len < sizeof(buffer)) {
        bytes_recvd = CFReadStreamRead(stream, buffer + recv_len,
            sizeof(buffer) - recv_len);

        /* Write bytes to output or file here */
    }
    recv_len += bytes_recvd;
}

You can now schedule the request in your main program's run loop, As the read stream is presented with data, your read callback will be invoked and will continue to perform on the data until the connection is closed or until the file has been completed:

CFReadStreamScheduleWithRunLoop(readStream,
    CFRunLoopGetCurrent(), kCFRunLoopCommonModes);

iPhone Home | All iPhone Tutorials | Latest iPhone Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Hello, I have a problem
I cant understand sampleFTPsimple that apple provide,
I dont know how to upload load and download file from FTP,
and i want look file document from FTP in my iphone.
Do you have others simple code can help me?
or give me some suggest ....
Thanks very much


View Tutorial          By: henry at 2012-01-10 07:53:05

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Jonathan Zdziarski
Making an FTP Connection in iPhone Application
Making an HTTP Connection in iPhone Application
Directory Structure for iPhone Applications

More Tutorials in iPhone
Making an HTTP Connection in iPhone Application
Making an FTP Connection in iPhone Application
Directory Structure for iPhone Applications
Multitasking in iPhone 4 - iOS 4

More Latest News
Most Viewed Articles (in iPhone )
Multitasking in iPhone 4 - iOS 4
Directory Structure for iPhone Applications
Making an FTP Connection in iPhone Application
Making an HTTP Connection in iPhone Application
Most Emailed Articles (in iPhone)
Multitasking in iPhone 4 - iOS 4
Directory Structure for iPhone Applications
Making an FTP Connection in iPhone Application
Making an HTTP Connection in iPhone Application