how to check network change iphone
I'm checking the network connection using this example from Apple:
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
开发者_StackOverflow NSLog(@"The internet is working via WIFI.");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
self.hostActive = YES;
break;
}
}
}
My intention is to first check that there's a connection and then send an email. The problem is that it takes too long to check if the the app can connect to a host let's say www.google.com it takes like almost a minute. Is there a faster way to check for this?
The other question is how to check if the connection was changed from wi-fi to 3G.
Thanks in advance!
Why do you want to determine the internet connection before emailing? Won't it be better if the email sits in the Outbox until a connection is established? That's generally how email clients work. Outlook or Gmail don't stop you from pressing the send button even if you are not connected. In the many MFMailComposer
implementations that I have seen, I have never seen any one checking for connection.
Regarding the 2nd question (again, I can't understand the reason behind this), I don't think there will be a callback that will tell you about WIFI changing to 3G. Naturally, you will have to keep polling. Keep calling checkNetworkStatus repeatedly and see which case is satisfied.
精彩评论