Reputation: 16851
I am using ASIHTTPRequest. I need to know how to do the following events using it;
The following are the way i should be checking for the availability of the internet, but in which event should i add it to address the following issues;
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
self.internetConnectionStatus = [[Reachability sharedReachability] internetConnectionStatus];
self.localWiFiConnectionStatus = [[Reachability sharedReachability] localWiFiConnectionStatus];
1.) Applications like foursquare pops out an alert as soon as the internet is lost ? How is this done, and in what event should i code this ? (The user might be performing a task, and suddenly the wifi goes off, then i should pop up a warning, saying no wifi available)
2.) As soon as the internet/wifi is back i need to refresh and update the data. How is this done? (it should be listening continuously, and once the internet is back it should update that view)
Upvotes: 2
Views: 534
Reputation: 27597
There is a notification posted by Reachability called kReachabilityChangedNotification
.
You can enforce the generation of that notification by using Reachability's
- (BOOL)startNotifier;
- (void)stopNotifier;
First register for that notification - for example in viewDidLoad
of a UIViewController
:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChangedNotificationSent:)
name:kReachabilityChangedNotification
object:nil];
Add the following method - for example to your UIViewController
implementation.
- (void)reachabilityChangedNotificationSent:(NSNotification *)notification
{
NSLog(@"reachability changed: %@", notification.userInfo);
}
Now simply invoke startNotifier
- for example in viewDidLoad
again and you should be informed whenever any changes happen.
[[Reachability sharedReachability] startNotifier];
Do not forget to remove yourself from that notification and to invoke stopNotifier
when done. Sticking to my example would mean that you should add this to your viewDidUnload
implementation.
[[Reachability sharedReachability] stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:kReachabilityChangedNotification
object:nil];
Upvotes: 3