View Full Version : How to ftp from the iPad/iPhone ?

Feb 1, 2011, 09:18 AM

I am trying to write a simple iPad application and I am facing the following problem, can any one point what I am doing wrong ?

I have a button bound to the following method (downLoad). When the button is pushed it should download a file (file.txt) from the internet.

It is the first time I do ftp in an iOS program, I just tried to put together pieces of information I found on the net.
So I might well have written nonsense. Please let me know. Is there a mistake in the code for the downLoad method I wrote ?
There must be (some mistake) since the readCallBack function does not seem to be called.

- (void) downLoad
CFStringRef UpGrdURL=CFSTR("ftp://mydomain.net/file.txt");
CFURLRef requestURL=CFURLCreateWithString(kCFAllocatorDefault,UpGrdURL,NULL);
CFURLRef requestLocalFile=CFURLCreateWithFileSystemPath(NULL, CFSTR("localFile.txt"),
kCFURLPOSIXPathStyle, false);
CFReadStreamRef readStream=CFReadStreamCreateWithFTPURL(kCFAllocatorDefault,requestURL);
CFWriteStreamRef writeStream=CFWriteStreamCreateWithFile(kCFAllocatorDefault,requestLocalFile);
CFWriteStreamOpen (writeStream);
if (CFWriteStreamGetStatus(writeStream)==kCFStreamStatusOpen) {
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @"Information box."
message: [NSString stringWithFormat:@"(CFWriteStreamOpen : kCFStreamStatusOpen."]
delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
[alertView show];
[alertView release];

MyStreamInfo XX;
CFStreamClientContext CTX = { 0, &XX, NULL, NULL, NULL };

CFStringRef UserName=CFSTR("myusername");
CFStringRef PassWord=CFSTR("mypassword");
CFReadStreamSetProperty (readStream,kCFStreamPropertyFTPUserName,UserName);
CFReadStreamSetProperty (readStream,kCFStreamPropertyFTPPassword,PassWord);
CFReadStreamSetProperty (readStream,kCFStreamPropertyFTPUsePassiveMode,NO);
// CFReadStreamOpen (readStream);

void readCallBack(
CFReadStreamRef stream,
CFStreamEventType eventType,
void *clientCallBackInfo)

In my header file I have :

#define kMyBufferSize 1024

typedef struct MyStreamInfo {
CFWriteStreamRef writeStream;
CFReadStreamRef readStream;
CFDictionaryRef proxyDict;
SInt64 fileSize;
UInt32 totalBytesWritten;
UInt32 leftOverByteCount;
UInt8 buffer[kMyBufferSize];
} MyStreamInfo;

Thanks in advance for any tip.