iPhone网络编程之监视网络连接

转载自http://hi.baidu.com/popln/blog/item/e6ed14884b2fa4a60f2444d1.html

  在当前的run循环中通过异步查询UIDevice当前的标志和网络状态,对网络连接状态进行监视。当连接状态变化时,观察者(observer)会以回调函数的形式接收到通知,从而做出相应的处理。

#import <SystemConfiguration/SystemConfiguration.h>
#include <netdb.h>
#import <dlfcn.h>
#import "UIDevice-Reachability.h"

@implementation UIDevice (Reachability)
SCNetworkConnectionFlags connectionFlags;
SCNetworkReachabilityRef reachability;

#pragma mark Checking Connections
+ (void) pingReachabilityInternal
{
    if (!reachability)
    {
        BOOL ignoresAdHocWiFi = NO;
        struct sockaddr_in ipAddress;
        bzero(&ipAddress, sizeof(ipAddress));
        ipAddress.sin_len = sizeof(ipAddress);
        ipAddress.sin_family = AF_INET;
        ipAddress.sin_addr.s_addr = htonl(ignoresAdHocWiFi ? INADDR_ANY : IN_LINKLOCALNETNUM);
        
        reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *)&ipAddress);
        CFRetain(reachability);
    }
    
    // Recover reachability flags
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(reachability, &connectionFlags);
    if (!didRetrieveFlags) 
    printf("Error. Could not recover network reachability flags\n"); } #pragma mark Monitoring reachability static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkConnectionFlags flags, void* info) { NSAutoreleasePool *pool = [NSAutoreleasePool new]; [(id)info performSelector:@selector(reachabilityChanged)]; [pool release]; }
// 开启网络连接监视
+ (BOOL) scheduleReachabilityWatcher: (id) watcher { if (![watcher conformsToProtocol:@protocol(ReachabilityWatcher)]) { NSLog(@"Watcher must conform to ReachabilityWatcher protocol. Cannot continue."); return NO; } [self pingReachabilityInternal]; SCNetworkReachabilityContext context = {0, watcher, NULL, NULL, NULL}; if(SCNetworkReachabilitySetCallback(reachability, ReachabilityCallback, &context)) { if(!SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetCurrent(), kCFRunLoopCommonModes)) { NSLog(@"Error: Could not schedule reachability"); SCNetworkReachabilitySetCallback(reachability, NULL, NULL); return NO; } } else { NSLog(@"Error: Could not set reachability callback"); return NO; } return YES; } + (void) unscheduleReachabilityWatcher { SCNetworkReachabilitySetCallback(reachability, NULL, NULL); if (SCNetworkReachabilityUnscheduleFromRunLoop(reachability, CFRunLoopGetCurrent(), kCFRunLoopCommonModes)) NSLog(@"Unscheduled reachability"); else NSLog(@"Error: Could not unschedule reachability"); CFRelease(reachability); reachability = nil; } @end
原文地址:https://www.cnblogs.com/pure/p/2469522.html