How to ftp from the iPad/iPhone ?

Discussion in 'iOS Programming' started by zaxonus, Feb 1, 2011.

  1. zaxonus, Feb 1, 2011
    Last edited by a moderator: Feb 1, 2011

    macrumors newbie

    Joined:
    Dec 23, 2010
    #1
    Hello,

    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.

    Code:
    - (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;
    XX.readStream=readStream;
    XX.writeStream=writeStream;
    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);
    CFReadStreamSetClient(readStream,
    kCFStreamEventHasBytesAvailable|kCFStreamEventErrorOccurred,
    readCallBack,&CTX);
    CFReadStreamScheduleWithRunLoop(readStream,CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
    }
    
    void readCallBack(
    CFReadStreamRef stream,
    CFStreamEventType eventType,
    void *clientCallBackInfo)
    {
    ……….
    }
    
    In my header file I have :

    Code:
    #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.
     

Share This Page