2011-11-02 14 views

Respuesta

12

Para esto necesita importar clases de accesibilidad en su proyecto.

Después de entonces: -

#import "Reachability.h" 

En ver DidLoad escritura: -

- (void)viewDidLoad { 
    Reachability *internetReach = [[Reachability reachabilityForInternetConnection] retain]; 
    [internetReach startNotifer]; 
    Reachability *wifiReach = [[Reachability reachabilityForLocalWiFi] retain]; 
    [wifiReach startNotifer]; 

    NetworkStatus netStatus1 = [internetReach currentReachabilityStatus]; 
    NetworkStatus netStatus2 = [wifiReach currentReachabilityStatus]; 
    if(netStatus1 == NotReachable && netStatus2 == NotReachable) 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"This feature requires an internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alertView show]; 
     [alertView release]; 
    } 
    else 
    {//wifi connection available; 
} 
} 
+1

Si el internat se desactiva la opción alcanzable, pero wi-fi ... Esto va a funcionar? – Oksana

1
First import Reachability files into your project. 

-(void)loginButtonTouched 
{ 
    bool success = false; 
    const char *host_name = [@"www.google.com" 
      cStringUsingEncoding:NSASCIIStringEncoding]; 

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName 
               (NULL, host_name); 
    SCNetworkReachabilityFlags flags; 
    success = SCNetworkReachabilityGetFlags(reachability, &flags); 
    bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && 
        !(flags & kSCNetworkFlagsConnectionRequired); 

    if (isAvailable) 
    { 
     NSLog(@"Host is reachable: %d", flags); 
     // Perform Action if Wifi is reachable and Internet Connectivity is present 
    } 
    else 
    { 
     NSLog(@"Host is unreachable"); 
     // Perform Action if Wifi is reachable and Internet Connectivity is not present 
    }  
} 

Cuando loginButtonTouched método se llama, comprobamos que www.google.com es accesible o no. SCNetworkReachabilityFlags devuelve indicadores que nos ayudan a comprender el estado de la conectividad a Internet. Si la variable isAvailable devuelve "verdadero", Host es Medios alcanzables Se puede acceder a Wifi y hay conectividad a Internet.

+0

Puede consultar el enlace para obtener más ayuda sobre el mismo tema: http://stackoverflow.com/questions/6705654/to-check-wifi-is-on-but-no-internet-connectivity/6705836#6705836 –

4

Encontré una gran línea de código para esto. Añadir la clase de accesibilidad a su proyecto y entonces usted puede hacer esto:

BOOL isConnectedProperly = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWiFi); 
Cuestiones relacionadas