2014年9月10日 星期三

使用Reachability判斷目前網路狀態

-(BOOL)connectedToNetworkWithShowAlert:(BOOL)ShowAlert
{
    BOOL result = true;
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
        default:
        case NotReachable:
            // 沒有網路
            result = false;
            break;
        case ReachableViaWWAN:
            // 使用3G網路
            break;
        case ReachableViaWiFi:
            // 使用WiFi網路
            break;
    }
    
    if(!result && ShowAlert)
    {
        UIAlertView* alertView = [[UIAlertView alloc]
                                  initWithTitle:nil
                                  message:NSLocalizedString(@"HaveNotInternet", @"HaveNotInternet")
                                  delegate:nil
                                  cancelButtonTitle:NSLocalizedString(@"yes", @"yes")
                                  otherButtonTitles:nil];
        
        [alertView show];
    }
    
    return result;

}

沒有留言:

張貼留言