View Single Post
  #1 (permalink)  
Old 09-05-2008, 10:10 AM
Xino Xino is offline
What's Jailbreak?
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
FTP usage in iPhone applications?

I'm trying to make a sort of camera application. If the user takes a photo, he has te possibility to upload this photo to a FTP server.

Now i've read and tried a lot but it still doesn't works.

This is the code I have at the moment, it's being executed when the user clicks the "Upload picture" button. I got this code from the iPhone developer reference created by Apple so it should work..?

Code:
- (IBAction)emailPicture {
	NSString *pictureLocation = [NSHomeDirectory() stringByAppendingString:@"/Documents/Pictures/"];
	pictureLocation = [pictureLocation stringByAppendingString:[NSString stringWithFormat:@"%lu", currentPictureViewing]];
	pictureLocation = [pictureLocation stringByAppendingString:@".jpg"];
	/*
	UIImage *selectedImage = [UIImage imageWithContentsOfFile:pictureLocation];
	NSData *imageData = UIImageJPEGRepresentation(selectedImage, 100);
	 */
	NSString *ftpServer = @"domain.com";
	NSString *ftpUsername = @"user";
	NSString *ftpPassword = @"pass";
	CFWriteStreamRef       writeStream;
    CFReadStreamRef        readStream;
    CFStreamClientContext  context = { 0, NULL, NULL, NULL, NULL };
    CFURLRef               uploadURL, destinationURL;
    CFStringRef            fileName;
    Boolean                success = true;
    MyStreamInfo           *streamInfo;
	
	
	/* Create a CFURL from the upload directory string */
	destinationURL = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef)ftpServer, NULL);
	/* Copy the end of the file path and use it as the file name. */
	CFURLRef tmpFileName = (CFURLRef)pictureLocation;
	fileName = CFURLCopyLastPathComponent(tmpFileName);
	 assert(fileName != NULL);
	/* Create the destination URL by taking the upload directory and appending the file name. */
	uploadURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, destinationURL, fileName, false);
    assert(uploadURL != NULL);
    CFRelease(destinationURL);
    CFRelease(fileName);
	/* Create a CFReadStream from the local file being uploaded. */
    readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,(CFURLRef)pictureLocation);
    assert(readStream != NULL);
	/* Create an FTP write stream for uploading operation to a FTP URL. If the URL specifies a
	 directory, the open will be followed by a close event/state and the directory will have been
	 created. Intermediary directory structure is not created. */
    writeStream = CFWriteStreamCreateWithFTPURL(kCFAllocatorDefault, uploadURL);
    assert(writeStream != NULL);
    CFRelease(uploadURL);
	/* Initialize our MyStreamInfo structure, which we use to store some information about the stream. */
    MyStreamInfoCreate(&streamInfo, readStream, writeStream);
    context.info = (void *)streamInfo;
	/* CFReadStreamOpen will return success/failure.  Opening a stream causes it to reserve all the
	 system resources it requires.  If the stream can open non-blocking, this will always return TRUE;
	 listen to the run loop source to find out when the open completes and whether it was successful. */
    success = CFReadStreamOpen(readStream);
    if (success) {
        
        /* CFWriteStreamSetClient registers a callback to hear about interesting events that occur on a stream. */
        //success = CFWriteStreamSetClient(writeStream, kNetworkEvents, MyUploadCallBack, &context);
        success = TRUE;
		if (success) {
			
            /* Schedule a run loop on which the client can be notified about stream events.  The client
			 callback will be triggered via the run loop.  It's the caller's responsibility to ensure that
			 the run loop is running. */
            CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
            
            MyCFStreamSetUsernamePassword(writeStream, (CFStringRef)ftpUsername, (CFStringRef)ftpPassword);
            MyCFStreamSetFTPProxy(writeStream, &streamInfo->proxyDict);
            
            /* CFWriteStreamOpen will return success/failure.  Opening a stream causes it to reserve all the
			 system resources it requires.  If the stream can open non-blocking, this will always return TRUE;
			 listen to the run loop source to find out when the open completes and whether it was successful. */
            success = CFWriteStreamOpen(writeStream);
            if (success == false) {
                fprintf(stderr, "CFWriteStreamOpen failedn");
                MyStreamInfoDestroy(streamInfo);
            }
        } else {
            fprintf(stderr, "CFWriteStreamSetClient failedn");
            MyStreamInfoDestroy(streamInfo);
        }
    } else {
        fprintf(stderr, "CFReadStreamOpen failedn");
        MyStreamInfoDestroy(streamInfo);
    }	
}
In the headerfile I also have the following code:

Code:
#define kMyBufferSize  32768

/* MyStreamInfo holds the state of a particular operation (download, upload, or 
 directory listing.  Some fields are only valid for some operations, as explained 
 by their comments. */
typedef struct MyStreamInfo {
	
    CFWriteStreamRef  writeStream;              // download (destination file stream) and upload (FTP stream) only
    CFReadStreamRef   readStream;               // download (FTP stream), upload (source file stream), directory list (FTP stream)
    CFDictionaryRef   proxyDict;                // necessary to workaround <rdar://problem/3745574>, per discussion below
    SInt64            fileSize;                 // download only, 0 indicates unknown
    UInt32            totalBytesWritten;        // download and upload only
    UInt32            leftOverByteCount;        // upload and directory list only, number of valid bytes at start of buffer
    UInt8             buffer[kMyBufferSize];    // buffer to hold left over bytes
	
} MyStreamInfo;
When I compile, some weird errors comes:
Code:
"_MyCFStreamSetFTPProxy" referenced from:
-[appTabBarController emailPicture] in appTabBarController.o
"_MyStreamInfoCreate", referenced from:
-[appTabBarController, emailPicture] in appTabBarController.o
etc..
etc..
I can't click on those errors but I think they have something to do with some warnings I get:

Code:
warning: implicit declaration of function 'MyCFStreamSetFTPProxy'
warning: implicit declaration of function 'MyStreamInfoCreate'
etc...
etc...
I'm developing with Xcode and included the CFNetwork framework into my project. Also imported CFNetwork.h into my headerfile but that also doesn't fix the warnings/errors...

Can you please help me?

Last edited by Xino; 09-05-2008 at 10:13 AM.
Digg StumbleUpon Delicious Reddit Newsvine Google Yahoo Thanks Reply With Quote