2011-11-02 11 views

उत्तर

12

इसके लिए आपको अपनी परियोजना में पहुंच योग्यता कक्षाएं आयात करने की आवश्यकता है।

तो बाद: -

#import "Reachability.h" 

में आप DidLoad लिखने देखने: -

- (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

तो अंतरराष्ट्रीय व्यापार से पहुंचा जा सकता है, लेकिन वाई-फाई विकल्प अक्षम किया गया है ... यह काम करेगा? – 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 
    }  
} 

loginButtonTouched विधि कहा जाता है जब हम जाँच करें कि www.google.com से पहुंचा जा सकता है या नहीं। SCNetworkReachabilityFlags झंडे लौटाता है जो हमें इंटरनेट कनेक्टिविटी की स्थिति को समझने में मदद करता है। यदि isAvailable परिवर्तनीय रिटर्न "सत्य" है तो होस्ट पहुंच योग्य माध्यम है वाईफ़ाई पहुंच योग्य है और इंटरनेट कनेक्टिविटी मौजूद है।

+0

आप उसी विषय पर अधिक सहायता के लिए लिंक का संदर्भ ले सकते हैं: http://stackoverflow.com/questions/6705654/to-check-wifi-is-on-but-no-internet-connectivity/6705836#6705836 –

4

इसके लिए कोड की एक बड़ी लाइन मिली। अपनी परियोजना के लिए गम्यता वर्ग जोड़ें और फिर आप यह कर सकते हैं:

BOOL isConnectedProperly = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWiFi); 
संबंधित मुद्दे