Reputation: 716
I am using ASIHTTPRequest to interact with my website. I am basically adding ASIHttpRequests to a queue. It all works fine if there is an internet connection. However, I guess the operation gets deleted if the request fails (ie, no internet connection)
Is there a way to queue and execute the request only when there's internet connection? If there's no internet connection, the request should be queued and automatically sent when the connection returns. If this is not possible, what is the best way to accomplish what I am trying here?
Thanks!
This is how I am setting up the queue right now :
NSURL *url = [NSURL URLWithString:@"http://www.tst.com.au/test.php?op=updateField"];
ASIFormDataRequest *startHourRequest = [ASIFormDataRequest requestWithURL:url];
NSDateFormatter *apiformatter = [[ [NSDateFormatter alloc] init] autorelease];
[apiformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *webdate = [apiformatter stringFromDate:date];
[startHourRequest setDelegate:appDelegate];
[startHourRequest setPostValue:webdate forKey:@"fieldvalue"];
[startHourRequest setPostValue:[appDelegate shiftuid] forKey:@"uid"];
[startHourRequest setPostValue:editAttribute forKey:@"fieldname"];
[startHourRequest setDidFinishSelector:@selector(requestDone:)];
[startHourRequest setDidFailSelector:@selector(requestWentWrong:)];
[[appDelegate queue] addOperation:startHourRequest];
Upvotes: 0
Views: 684
Reputation: 5552
Yes you can do that quite easily with the Reachability class:
First off you will check internet reachability before you send the initial request:
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable) {
//Do your request
}
else {
//save your url to an array for use when the internet returns
}
and within the view controller or wherever you are sending the requests from you do this to listen for internet changes
- (void)viewDidLoad
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(networkStatusChanged:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
}
- (void)networkStatusChanged:(NSNotification *)notice {
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
if (internetStatus != NotReachable && [yourUrlsToSend count] > 0) {
//send off your requests now that you have internet
}
}
This all depends on including the reqchability files from apple which can be found here: enter link description here . Hope that helps
Upvotes: 1