IOS testing network status

Source: Internet
Author: User

[Html] /////// determine the network status when you first open the connection-(int) connectedToNetwork {NSString * kindStr; int kind; // create a zero address, the address 0.0.0.0 indicates querying the network connection status of the local machine. 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, (stru Ct sockaddr *) & zeroAddress); SCNetworkReachabilityFlags flags; // obtain the connection flag BOOL didRetrieveFlags = downlink (defaultRouteReachability, & flags); CFRelease (defaultRouteReachability ); // if the connection flag cannot be obtained, the network cannot be connected. if (! DidRetrieveFlags) {kindStr = @ "failed to get information"; kind =-1;} else {// determine BOOL isReachable = flags & kSCNetworkFlagsReachable Based on the obtained connection flag; BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; if (isReachable &&! NeedsConnection) = YES) {// can connect to the network if (flags & kSCNetworkReachabilityFlagsIsWWAN) = kSCNetworkReachabilityFlagsIsWWAN) {kindStr = @ "mobile network"; kind = 0 ;} else {kindStr = @ "wifi connected network"; kind = 1 ;}} else {kindStr = @ "cannot connect to network"; kind =-1 ;}} return kind ;} ///////////// set the network listener-(void) SetJianCeNetwork {// enable the network condition listener [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (reachabilityChang Ed :) name: kReachabilityChangedNotification object: nil]; hostReach = [[Reachability reachabilityWithHostname: @ "www.xtox.net"] retain]; // You can initialize [hostReach startNotifier] in multiple forms; // start listening. A run loop will be started} // connection change-(void) reachabilityChanged: (NSNotification *) note {Reachability * curReach = [note object]; NSParameterAssert ([curReach isKindOfClass: [Reachability class]); [self updateInterfaceWithReachab Ility: curReach];} // handle the situation after the network connection changes-(void) updateInterfaceWithReachability: (Reachability *) curReach {// action to respond to connection changes. NetworkStatus status = [curReach currentReachabilityStatus]; if (status = NotReachable) {// if the network is not connected, the actual isNetType =-1 is displayed; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: nil message: @ "network connection failed. Check the network" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert show]; [alert release];} else if (status = ReachableViaWiFi | status = ReachableViaWWAN) {if (status = ReachableViaWWAN ){ IsNetType = 0;} else if (status = ReachableViaWiFi) {isNetType = 1;} // NSThread * initThread = [[NSThread alloc] initWithTarget: self selector: @ selector (InitThreadFunc :) object: self]; [initThread start]; return ;}}- (void) InitThreadFunc :( id) sender {// isNetType = [self connectedToNetwork]; if (isNetType! =-1) {if ([g_json Test] = YES) {[g_json UserLogin: 0 pass: nil]; [self GetUpdate]; return ;} else {isNetType =-1 ;}}}

Related Article

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.