2011-04-27 8 views
11

Tengo una aplicación que se basa completamente en la web y necesita una conexión a Internet para navegar. Básicamente, un sitio web visto a través de UIWebView.No hay conexión a Internet Manejo en UIWebView y NSURLRequest

Necesito poder decirle al usuario que no se pueden cargar páginas si no tienen conexión a Internet. ¿Hay alguna manera simple de hacerlo? Tal vez un cheque si NSURLRequest falló?

Saludos

Respuesta

5

me gustaría ver el código Reachability muestra de Apple para implementar esta forma fiable. Una ventaja de este enfoque es que puede notificar al usuario sobre el estado actual de la red, incluso si el usuario no está haciendo clic en ningún enlace en la vista web.

0

1> Agregar SystemConfiguration.framework a su proyecto

2> importación siguientes archivos .h en su archivo Connection.h

#import <sys/socket.h> 
#import <netinet/in.h> 
#import <SystemConfiguration/SystemConfiguration.h> 

3> declare el siguiente método de clase en su archivo Connection.h

+(BOOL)hasConnectivity; 

4> definir este método en su Connection.m archivo

+(BOOL)hasConnectivity { 

struct sockaddr_in zeroAddress; 
bzero(&zeroAddress, sizeof(zeroAddress)); 
zeroAddress.sin_len = sizeof(zeroAddress); 
zeroAddress.sin_family = AF_INET; 

SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress); 
if(reachability != NULL) { 
    //NetworkStatus retVal = NotReachable; 
    SCNetworkReachabilityFlags flags; 
    if (SCNetworkReachabilityGetFlags(reachability, &flags)) { 
     if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 
     { 
      // if target host is not reachable 
      return NO; 
     } 

     if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 
     { 
      // if target host is reachable and no connection is required 
      // then we'll assume (for now) that your on Wi-Fi 
      return YES; 
     } 


     if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0) || 
      (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 
     { 
      // ... and the connection is on-demand (or on-traffic) if the 
      //  calling application is using the CFSocketStream or higher APIs 

      if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 
      { 
       // ... and no [user] intervention is needed 
       return YES; 
      } 
     } 

     if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) 
     { 
      // ... but WWAN connections are OK if the calling application 
      //  is using the CFNetwork (CFSocketStream?) APIs. 
      return YES; 
     } 
    } 
} 

return NO; 
} 
Cuestiones relacionadas