當您檢查連接性時,您需要更嚴格。在收到可達性更改通知時添加更多條件。
檢查以下條件:
- (void)reachabilityDidChange:(NSNotification *)notification {
// Reachbility for internet
Reachability *reachability = (Reachability *)[notification object];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:
{
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
// Reachbility for host
Reachability *hostReachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus hostStatus = [hostReachability currentReachabilityStatus];
switch (hostStatus) {
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
什麼是第一和第二switch語句之間的區別?兩者似乎都在檢查'[reachability currentReachabilityStatus]' – sooper 2015-03-30 14:48:13
@sooper:謝謝你:)我確實錯過了那一點,編輯答案。 – Kampai 2015-03-31 05:57:59