reachability ios网络连接判断

发布时间:2020-06-26 20:59:27 作者:卓行天下
来源:网络 阅读:868

reachability ios网络连接判断 

//Reachability.h

import 

import 

typedef enum { NotReachable = 0, ReachableViaWiFi, ReachableViaWWAN } NetworkStatus;

define kReachabilityChangedNotification @"kNetworkReachabilityChangedNotification"

@interface Reachability: NSObject { BOOL localWiFiRef; SCNetworkReachabilityRef reachabilityRef; }

//reachabilityWithHostName- Use to check the reachability of a particular host name. 

//reachabilityWithAddress- Use to check the reachability of a particular IP address. 

//reachabilityForInternetConnection- checks whether the default route is available.
// Should be used by applications that do not connect to a particular host

//reachabilityForLocalWiFi- checks whether a local wifi connection is available.

//Start listening for reachability notifications on the current run loop

//Reachability.m

import 

import 

import 

import 

import 

import 

import 

import "Reachability.h"

define kShouldPrintReachabilityFlags 1

static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment) {

if kShouldPrintReachabilityFlags

NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n",
        (flags & kSCNetworkReachabilityFlagsIsWWAN)                  ? 'W' : '-',
        (flags & kSCNetworkReachabilityFlagsReachable)            ? 'R' : '-',

        (flags & kSCNetworkReachabilityFlagsTransientConnection)  ? 't' : '-',
        (flags & kSCNetworkReachabilityFlagsConnectionRequired)   ? 'c' : '-',
        (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)  ? 'C' : '-',
        (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
        (flags & kSCNetworkReachabilityFlagsConnectionOnDemand)   ? 'D' : '-',
        (flags & kSCNetworkReachabilityFlagsIsLocalAddress)       ? 'l' : '-',
        (flags & kSCNetworkReachabilityFlagsIsDirect)             ? 'd' : '-',
        comment
        );

endif

}

@implementation Reachability static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) {

pragma unused (target, flags)

NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");//We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively// in case someon uses the Reachablity object in a different thread.NSAutoreleasePool* myPool = [[NSAutoreleasePool alloc] init];

Reachability* noteObject = (Reachability*) info;// Post a notification to notify the client that the network reachability changed.[[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];

[myPool release];

}

pragma mark Network Flag Handling

//appcontroller

//网络状态改变

} -(void) updateInterfaceWithReachability:(Reachability*) curReach { NetworkStatus netStatus = [curReach currentReachabilityStatus]; //BOOL connectionRequired= [curReach connectionRequired]; if(firstComeIn){ firstComeIn=false; return; } switch (netStatus) { case NotReachable: //没有连接 {

        break;
    }    case ReachableViaWWAN: //3g/GPRS
    {        break;
    }    case ReachableViaWiFi:  //wifi
    {        break;
    }
}


推荐阅读:
  1. android 中怎么判断网络连接
  2. 使用php怎么对网络连接进行判断

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

reachability ty ea

上一篇:ionic侧边栏 ion-side-menus 以及 ion-tap结合侧边 栏详解

下一篇:Visual Studio Code-批量添加或删除注释行

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》