Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS basic FTP setup; Read and Write Stream

I'm attempting to create an iOS 5 app with some very basic FTP functionality and need some guidance. It will be connecting to a device on a local network and performing read/write actions with .dat/txt files. I've done some searching for the past few days and have seen various recommendations but nothing simple enough that I can pick up and quickly modify for my personal use.

My questions are these:

  1. Are there any tutorials/sample code that you could recommend to me?
  2. What frameworks and classes should I be working with for basic read/write operations?

Lastly, I should mention that I have given a considerable amount of time to analyzing the SimpleFTPSample from Apple but the sample code is giving "Connection Failure" and "Stream Open Error" notices for each example, so I'm a bit wary of its usefulness.

Forgive me if this has been answered elsewhere. All of the related posts have pieces of the answer I need, but not the whole thing. Thank you in advance!

EDIT for clarity: A well-defined example or step-by-step tutorial is what I would really like. My own Google searches have turned up nothing and I am desperately in need of some guidance here.

UPDATE: I posted this question long ago but have continued using the FTPHelper mentioned in the accepted answer. I recently brushed the dust off the old project and realized there was a minor memory leak in FTPHelper's fetch function that can be an app-killer if called repeatedly. If anybdy stumbles across this question and chooses to use FTPHelper, be sure to add the CFRelease line seen in the code below.

- (void) fetch: (NSString *) anItem
{
    if (!self.uname || !self.pword) COMPLAIN_AND_BAIL(@"Please set user name and password first");
    if (!self.urlString) COMPLAIN_AND_BAIL(@"Please set URL string first");

    NSString *ftpRequest = [NSString stringWithFormat:@"%@/%@", self.urlString, [anItem stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
/*  CFShow(ftpRequest); */
    NSString *writepath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    self.filePath = [writepath stringByAppendingPathComponent:anItem];
    CFURLRef writeURL = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8 *) [writepath UTF8String], [writepath length], NO); 
    MySimpleDownload((CFStringRef)ftpRequest, writeURL, (CFStringRef) self.uname, (CFStringRef)self.pword);
    CFRelease(writeURL);//ADD THIS LINE TO FIX MEMORY LEAK 
}
like image 890
Squatch Avatar asked Mar 12 '12 18:03

Squatch


2 Answers

The SimpleFTPSample app is running perfect, probably there is an issue that you can't see. What I can recommend you (except Apple's example) is to check THIS example which contains a helper class for all basic FTP operations. One thing to be aware of is iOS 5 ARC. Both Apple's example and the one I linked are for older iOS versions.

There are basically 2 ways to use them in iOS 5 - by telling the compiler to not use ARC by adding -fno-objc-arc flag in [Your project] -> TARGETS -> [Your app] -> Build Phases -> Compile Sources -> [Your file], or by using the built-in tool in Xcode for converting to ARC.

I personally have tested only the first method and it works for me.

If this does not help you I can write an example, but unfortunately today I am very busy.

UPDATED:

The basic mechanism is to use [FTPHelper list:THE_FTP_URL] to list the content of a folder, then create one list with the content and depending on the type (file or folder) download using [FTPHelper download: THE_FTP_URL_WITH_THE_FILENAME_FROM_LISTING]. From here you have to implement

- (void) downloadFinished
{
    //do the reading depending on the file type
    NSData *data = [NSData dataWithContentsOfFile:[FTPHelper sharedInstance].filePath];
}

The uploading is achieved in a similar way - using [FTPHelper upload:FILE_TO_UPLOAD] with a file from the filesystem.

like image 148
o15a3d4l11s2 Avatar answered Nov 08 '22 06:11

o15a3d4l11s2


There are many libraries which you could use and they are working great. :)

For example:

http://www.chilkatsoft.com/ftp-objc.asp

http://code.google.com/p/ios-ftp-server/

I recommend using them, because coding one by yourself would take a lot of time :) One thing to remember, as o15a3d4l11s2 said, is to be aware of ARC. If you use it don't forget to add build flags to libraries which aren't ARC.

like image 33
Dominik Hadl Avatar answered Nov 08 '22 05:11

Dominik Hadl