`
风尘哥哥
  • 浏览: 3378 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Reachability

阅读更多
//  Reachability.h 2.0

#import <Foundation/Foundation.h>
#import <netinet/in.h>
#import <SystemConfiguration/SystemConfiguration.h>

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.
+ (Reachability*) reachabilityWithHostName: (NSString*) hostName;

//reachabilityWithAddress- Use to check the reachability of a particular IP address.
+ (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;

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

//reachabilityForLocalWiFi- checks whether a local wifi connection is available.
+ (Reachability*) reachabilityForLocalWiFi;

//Start listening for reachability notifications on the current run loop
- (BOOL) startNotifier;
- (void) stopNotifier;

- (NetworkStatus) currentReachabilityStatus;
//WWAN may be available, but not active until a connection has been established.
//WiFi may require a connection for VPN on Demand.
- (BOOL) connectionRequired;
@end

//Reachability.m

#import <sys/socket.h>
#import <netinet/in.h>
#import <netinet6/in6.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#import <CoreFoundation/CoreFoundation.h>
#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 %sn",
          (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];
}

- (BOOL) startNotifier
{
    BOOL retVal = NO;
    SCNetworkReachabilityContext    context = {0, self, NULL, NULL, NULL};
    if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context))
    {
        if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
        {
            retVal = YES;
        }
    }
    return retVal;
}

- (void) stopNotifier
{
    if(reachabilityRef!= NULL)
    {
        SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    }
}

- (void) dealloc
{
    [self stopNotifier];
    if(reachabilityRef!= NULL)
    {
        CFRelease(reachabilityRef);
    }
    [super dealloc];
}

+ (Reachability*) reachabilityWithHostName: (NSString*) hostName;
{
    Reachability* retVal = NULL;
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
    if(reachability!= NULL)
    {
        retVal= [[[self alloc] init] autorelease];
        if(retVal!= NULL)
        {
            retVal->reachabilityRef = reachability;
            retVal->localWiFiRef = NO;
        }
    }
    return retVal;
}

+ (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;
{
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);
    Reachability* retVal = NULL;
    if(reachability!= NULL)
    {
        retVal= [[[self alloc] init] autorelease];
        if(retVal!= NULL)
        {
            retVal->reachabilityRef = reachability;
            retVal->localWiFiRef = NO;
        }
    }
    return retVal;
}

+ (Reachability*) reachabilityForInternetConnection;
{
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    return [self reachabilityWithAddress: &zeroAddress];
}

+ (Reachability*) reachabilityForLocalWiFi;
{
    struct sockaddr_in localWifiAddress;
    bzero(&localWifiAddress, sizeof(localWifiAddress));
    localWifiAddress.sin_len = sizeof(localWifiAddress);
    localWifiAddress.sin_family = AF_INET;
    // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
    localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
    Reachability* retVal = [self reachabilityWithAddress: &localWifiAddress];
    if(retVal!= NULL)
    {
        retVal->localWiFiRef = YES;
    }
    return retVal;
}

#pragma mark Network Flag Handling

- (NetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags
{
    PrintReachabilityFlags(flags, "localWiFiStatusForFlags");
   
    BOOL retVal = NotReachable;
    if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect))
    {
        retVal = ReachableViaWiFi;
    }
    return retVal;
}

- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags
{
//    PrintReachabilityFlags(flags, "networkStatusForFlags");
    if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
    {
        // if target host is not reachable
        return NotReachable;
    }
   
    BOOL retVal = NotReachable;
   
    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
        retVal = ReachableViaWiFi;
    }
   
   
    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
            retVal = ReachableViaWiFi;
        }
    }
   
    if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
    {
        // ... but WWAN connections are OK if the calling application
        //     is using the CFNetwork (CFSocketStream?) APIs.
        retVal = ReachableViaWWAN;
    }
    return retVal;
}

- (BOOL) connectionRequired;
{
    NSAssert(reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef");
    SCNetworkReachabilityFlags flags;
    if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
    {
        return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
    }
    return NO;
}

- (NetworkStatus) currentReachabilityStatus
{
    NSAssert(reachabilityRef != NULL, @"currentNetworkStatus called with NULL reachabilityRef");
    NetworkStatus retVal = NotReachable;
    SCNetworkReachabilityFlags flags;
    if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
    {
        if(localWiFiRef)
        {
            retVal = [self localWiFiStatusForFlags: flags];
        }
        else
        {
            retVal = [self networkStatusForFlags: flags];
        }
    }
    return retVal;
}

@end
分享到:
评论

相关推荐

    ios Reachability

    Reachability是苹果官方提供的一款用于检测iOS设备网络状态的示例代码库,它极大地简化了开发者在应用中检查网络连通性的过程。这个DEMO包含了详细的实现方式和使用示例,帮助开发者理解如何在自己的应用中集成网络...

    Reachability 网络判断库文件

    Reachability 是一个在iOS和macOS开发中广泛使用的开源库,由Tony Million 创建,用于检测设备的网络连通状态和网络类型。这个库基于Apple的SystemConfiguration框架,它提供了简单易用的接口来帮助开发者判断应用...

    Reachability使用说明<转载>

    Reachability是iOS开发中一个非常重要的工具,它用于检测设备的网络可达性,例如Wi-Fi、蜂窝数据等网络状态的变化。这个工具是由斯坦福大学的Scotty Loveless开发的,并且在iOS开发者社区中广泛使用。这篇博客文章...

    Reachability 网络状态的使用和封装,更加方便使用

    Reachability 是 iOS 开发中一个重要的工具类,用于检测设备的网络状态,包括无网络、WiFi、蜂窝数据等。这个工具最初由 Tony Million 设计并实现,现在已经被广泛使用在许多 iOS 应用中。在本文中,我们将深入探讨 ...

    iOS中使用 Reachability 检测网络

    Reachability是Apple提供的一款工具,用于检测设备的网络可达性,即是否可以连接到互联网。这篇博客文章将深入探讨如何在iOS项目中使用Reachability来监控网络状态,并提供相关的源码分析。 一、 Reachability简介 ...

    Reachability网络状态

    Reachability是iOS开发中用于检测网络连接状态的一个重要工具,由Tony Million开发并维护,它提供了简单的方法来判断设备当前的网络连通性。在iOS 11及更高版本中,由于苹果对用户隐私的加强保护,不再推荐使用Key-...

    swift-Reachability.swift使用Swift写的第三方网络检测类

    Reachability.swift 是一款由Ashley Mills开发的著名第三方库,专为Swift编程语言设计,用于检测iOS、macOS、tvOS以及watchOS等Apple平台上的网络可达性。这个库简化了开发者判断当前设备网络状态的过程,包括是否...

    Reachability.zip

    Reachability是iOS开发中一个非常重要的概念,它用于检测设备的网络可达性,即设备是否能够连接到互联网以及通过何种方式连接。这个功能在开发任何需要网络通信的应用时都至关重要,因为它可以帮助应用根据网络状态...

    swift-Reach一个现代Reachability网络库

    **Swift-Reach:现代Reachability网络库详解** Swift-Reach 是一个专为Swift编程语言设计的现代化网络可达性库,它的主要目标是帮助开发者轻松地检测iOS、macOS、tvOS或watchOS应用的网络状态。这个库是基于Apple的...

    iOS_Reachability

    Reachability 是 iOS 开发中一个重要的知识点,用于检测设备当前的网络状态,包括是否连接到互联网,以及连接的类型(例如蜂窝数据还是 Wi-Fi)。这个例子是苹果官方提供的,对于开发者来说,是一个非常有价值的参考...

    Reachability.swift:使用 Swift 闭包实现的网络状态检查库,可以取代 Apple 的 Reachability 库.zip

    Reachability.swift:使用 Swift 闭包实现的网络状态检查库,可以取代 Apple 的 Reachability 库.zip,Replacement for Apple's Reachability re-written in Swift with closures

    IOS ReachAbility

    IOS 网络编程 ReachAbility头文件和源文件,用于判定网络的连接情况

    iOS 网络检查 Reachability

    Reachability是Apple提供的一个库,用于检测设备当前的网络可达性,包括Wi-Fi、蜂窝数据以及互联网是否可达。这个库可以帮助开发者及时响应网络状态的变化,比如在网络断开时提示用户或者暂停网络依赖的操作。 首先...

Global site tag (gtag.js) - Google Analytics