IPhone network connection detection (Wifi, 3G, Edge), features a bit like Reachability)

Source: Internet
Author: User

This class can be used to check whether a user is connected to the internet. The usage is very simple. There is only one method, and YES or NO is returned.

A simple example:

if ([Connection isConnected]) {  ...  }     else {  ...  }  

Class header file

//   //  Connection.h     //   #import <Foundation/Foundation.h>   #import <SystemConfiguration/SystemConfiguration.h>   #import <netinet/in.h>   #import <arpa/inet.h>   #import <netdb.h>     @interface Connection : NSObject {   }     + (BOOL) isConnected;     @end 

Class implementation file

//   //  Connection.m  //     #import "Connection.h"     @implementation Connection     + (BOOL) isConnected {       // Create zero addy       struct sockaddr_in zeroAddress;       bzero(&zeroAddress, sizeof(zeroAddress));       zeroAddress.sin_len = sizeof(zeroAddress);       zeroAddress.sin_family = AF_INET;       // Recover reachability flags       SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);       SCNetworkReachabilityFlags flags;       BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);       CFRelease(defaultRouteReachability);       if (!didRetrieveFlags) {           NSLog(@"Error. Could not recover network reachability flags");           return NO;       }       BOOL isReachable = flags & kSCNetworkFlagsReachable;       BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;       BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;       NSURL *testURL = [NSURL URLWithString:@"http://www.google.com/"];       NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];       NSURLConnection *testConnection = [[[NSURLConnection alloc] initWithRequest:testRequest delegate:self] autorelease];       return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;   }   @end  

Finally, do not forget to add the necessary frameworks: SystemConfiguration and libz.1.1.3.dylib.

The Reachability mentioned in the title also provides the network detection function. You can reference: http://www.raddonline.com/blogs/geek-journal/iphone-sdk-testing-network-reachability/

Http://developer.apple.com/iphone/library/samplecode/Reachability/index.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.