简体   繁体   中英

iOS Deciding to stop accessing server after fixed time

My question might have a very easy answer but I am not going anywhere on Google with the keywords I am using. Let me explain, it surely will be clear to one of you.

In my app, I am uploading a string to a server. If the connection is successful, the user gets a "done" message and if not, I tell him what to do next.

I was testing this afternoon with the "no network simulator" and it works well, but I had to wait may be 90 seconds before I get the message "no connection available". I don't know for how long it tries to connect in real life, but I found the wait really annoying and wouldn't like my user to be in such a situation.

My question is : I want the app to try and connect to the server for say 20 seconds and if after that time it hasn't succeeded, it stops trying and the user decides if he wants to try a second time or not ?

My apologies in advance, English isn't my mother tongue.

Thank you for pointing me to the right direction.

You should set timeout interval in session configuration

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest = 20;
configuration.timeoutIntervalForResource = 20;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

// in case if you are using AFNetworking
// AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM