0
votes

I'm working on an iPhone app that uses data stored remotely. The app is to be used by our client only (no app store) and I'm not allow to specify the app's context.

The only context I can tell you is that the app allows a code to be entered, and downloads the data related to that code.

Our client told us that some times we'll need to use the app on places were internet isn't accessible or too slow.

The solution that has been drawn is the following:

  1. We'll try to download the data on a local FTP server, which is actually just a router sharing a directory with the code as its name, accessible by a known IP (e.g., ftp://192.168.1.1/some-path).

  2. If it fails, it tries to download from the web server.

I've found an awesome library called FTPManager by Nico Kreipke. (click here for its GitHub)

Unfortunately, when the FTP address isn't available, it take about one minute to timeout and move to the second step.

Does anyone can help out on reducing this timeout? Maybe making a quick ping to the IP? If so, how to do it?

Thanks for your time,
Tiago

Some More Info

I've tried a solution inspired by the answer of Rauru Ferro. But unfortunately it didn't help. The code used for testing follows:

NSString *ftp = [NSString stringWithFormat:@"ftp://user:password@%@/sda1/%@", ip, code];
NSURL *url = [NSURL URLWithString:ftp];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

I've tried several timeout values, but to no avail. It always ends with a timeout error. However, the downloading code is able to download data from the router's shared directory (using the same credentials).

2

2 Answers

0
votes

Try to use this code (is build for a http connection with POST httpMethod, but is a beginnning):

NSURL *url = [NSURL URLWithString:yourURL];
NSMutableURLRequest *request = [NSMutableURLRequest
                                 requestWithURL:url
                                 cachePolicy:NSURLRequestReloadIgnoringCacheData
                                 timeoutInterval:20.0];
[request setHTTPMethod:@"POST"];
0
votes

There are a few things to take into consideration when you connect to a server. The first thing to determine will be, "did I connect to the server" and the second will be "how long did it take for the server to respond?".

With FTP, the low level tcp/ip connection is first made to the server and then the server will respond with the initial 2xx level Welcome/Hello message.

When using a high level class interface such as NSMutableURLRequest, it might be difficult to distinguish between a connection timeout, and a server response timeout. It's possible that you are connecting to the server, but the server is bogged down and just very slow to respond with the Welcome message.

My recommendation would be to go low-level and use a generic BSD socket object, create the socket and then connect to the server. You can also use the low level setsockopt() call to limit the amount of time that the connect() call will wait for a connection. With FTP, you could also optionally do a recv() to retrieve the welcome message from the server. You could set a different timeout on this call to ensure that the server responds in a timely fashion.

At a slightly higher level, the CFSocket class will give you a bit more encapsulation from the BSD layer, but you should still be able to break out the difference between the connection time and the initial response time.

Hope that helps! Michael