Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-28-2010, 02:01 PM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 10
ccramer is on a distinguished road
Default Problem With CFWriteStreamRef

Is anybody out there having any problems with opening a write stream connected to an FTP server? I can get a directory listing fine, but the code which has been working to create and open a CFWriteStreamRef is failing when trying to write to a local directory from an ftp server.
It worked fine until OS4.0 and it also works on the 4.0 simulator.

Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documents = [[paths objectAtIndex:0] stringByAppendingString:@"/Inbox/"];




NSMutableArray *files_to_download = [[NSMutableArray alloc] init]; //stores files

NSString *downloadAddress = @"ftp://><an address>/Downloads/";
                         

//try and get listing

Boolean status = MySimpleDirectoryListing((CFStringRef)downloadAddress, CFSTR("userName"), CFSTR("password"));
//***Works Fine and Returns Listing***
if (!status) NSLog(@"(MySimpleDirectoryListing) ERROR: failed to connect");

if (status) CFRunLoopRun();

NSInteger i = 0;

NSInteger count = files_to_download.count;

for(i;i<count;i++)
{
     //be sure to append the directory and the filename to the address.

     NSString *address = [@"ftp://><an address>/" stringByAppendingString:[@"/Downloads/" stringByAppendingString:(NSString *)[files_to_download objectAtIndex:i]]];
     NSURL *u = [[NSURL alloc] initFileURLWithPath:documents];

     status = iDownload((CFStringRef)address, (CFURLRef)u, CFSTR("userName"), CFSTR("password"));

     [u release];

     if (!status) NSLog([@"(iDownload) ERROR: failed to download " stringByAppendingString:address]);

     if (status) CFRunLoopRun();
}
Code:
static Boolean

iDownload(CFStringRef urlString, CFURLRef destinationFolder, CFStringRef username, CFStringRef password)

{

    CFReadStreamRef        readStream;

    CFWriteStreamRef       writeStream;

    CFStreamClientContext  context = { 0, NULL, NULL, NULL, NULL };

    CFURLRef               downloadPath, downloadURL;

    CFStringRef            fileName;

    Boolean                success = true;

    iStreamInfo           *streamInfo;



    downloadURL = CFURLCreateWithString(kCFAllocatorDefault, urlString, NULL);

    /* Copy the end of the file path and use it as the file name. */

    fileName = CFURLCopyLastPathComponent(downloadURL);

     assert(downloadURL != NULL);     

    /* Create the downloadPath by taking the destination folder and appending the file name. */

    downloadPath = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, destinationFolder, fileName, false);

     CFRelease(fileName);

     assert(downloadPath != NULL);

    /* Create a CFWriteStream for the file being downloaded. */

    writeStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault, downloadPath);

    CFRelease(downloadPath);

     assert(writeStream != NULL);

     

    /* CFReadStreamCreateWithFTPURL creates an FTP read stream for downloading from an FTP URL. */

    readStream = CFReadStreamCreateWithFTPURL(kCFAllocatorDefault, downloadURL);

    CFRelease(downloadURL);

    assert(readStream != NULL);


    iStreamInfoCreate(&streamInfo, readStream, writeStream);

    context.info = (void *)streamInfo;


    success = CFWriteStreamOpen(writeStream);     

    if (success) {
               
          //read the stream using callback
    } 
    else {

        fprintf(stderr, "CFWriteStreamOpen failed\n");

        iStreamInfoDestroy(streamInfo);

    }     

    return success;

}
Code:
#define kMyBufferSize  32768

typedef struct iStreamInfo {

     

    CFWriteStreamRef  writeStream;        

    CFReadStreamRef   readStream;               

    SInt64            fileSize;                 

    UInt32            totalBytesWritten;     

    UInt32            leftOverByteCount;        

    UInt8             buffer[kMyBufferSize];

    CFDictionaryRef   proxyDict;

     

} iStreamInfo;


static void

iStreamInfoCreate(iStreamInfo ** info, CFReadStreamRef readStream, CFWriteStreamRef writeStream)

{

    iStreamInfo * streamInfo;



    streamInfo = malloc(sizeof(iStreamInfo));



    streamInfo->readStream        = readStream;

    streamInfo->writeStream       = writeStream;

    streamInfo->fileSize          = 0;

    streamInfo->totalBytesWritten = 0;

    streamInfo->leftOverByteCount = 0;

     

    *info = streamInfo;

}
ccramer is offline   Reply With Quote
Old 06-30-2010, 12:00 PM   #2 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 10
ccramer is on a distinguished road
Default

Maybe it is something in the new OS that blocks this stream?
ccramer is offline   Reply With Quote
Old 07-07-2010, 10:31 AM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 10
ccramer is on a distinguished road
Default

It seems as though I am having problems saving to subdirectories that I created within the Documents directory. I couldn't stream to the subdirectory so I decided to stream to the documents directory and then try to copy the files using filemanager. No such luck. I can write a file, such as a plist, to the directories. I have checked paths and made sure directories exist. Any ideas on why this broke in IPhone OS 4.0?
ccramer is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 324
7 members and 317 guests
anothermine, Chickenrig, Domele, givensur, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0