美文网首页
iOS AFNetworking

iOS AFNetworking

作者: zzz_zk | 来源:发表于2018-02-08 20:49 被阅读0次

    AFNetworkReachabilityManager

    这个类主要是监听当前的网络状态,并通过通知广播给调用者。

    网络状态有如下几种,分别对应的未知、断网、移动网络、Wi-Fi

    typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
        AFNetworkReachabilityStatusUnknown          = -1,
        AFNetworkReachabilityStatusNotReachable     = 0,
        AFNetworkReachabilityStatusReachableViaWWAN = 1,
        AFNetworkReachabilityStatusReachableViaWiFi = 2,
    };
    

    官方已经提供一个句柄来标识网络地址状态,就是它”SCNetworkReachabilityRef”,先看看AFNetworkReachabilityManager其中几个初始化方法。

    • 第一个方法使用domain参数来初始化一个实例,同理第二个方法使用address来初始化实例。这里需要注意,如果使用非Cocoa框架的类创建一个实例,需要手动对这个对象进行内存管理。
    • 第四个方法是作者推荐用的初始化方法,根据当前iOS开发最低支持编译版本是否大于9.0来创建对应的网络协议对象,大于9.0则创建IPv6,否则创建IPv4
    + (instancetype)managerForDomain:(NSString *)domain {
        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [domain UTF8String]);
        AFNetworkReachabilityManager *manager = [[self alloc] initWithReachability:reachability];
        CFRelease(reachability);
        return manager;
    }
    
    + (instancetype)managerForAddress:(const void *)address {
        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)address);
        AFNetworkReachabilityManager *manager = [[self alloc] initWithReachability:reachability];
        CFRelease(reachability);
        return manager;
    }
    
    - (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability {
        self = [super init];
        if (!self) {
            return nil;
        }
        _networkReachability = CFRetain(reachability);
        self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
        return self;
    }
    
    + (instancetype)manager {
    // __IPHONE_OS_VERSION_MIN_REQUIRED 表示当前开发环境最低支持的版本号
    // 编译时判断如果当前iOS开发环境版本低于9.0则使用IPv4,否则使用IPv6的结构体
    
    #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
        struct sockaddr_in6 address;
        bzero(&address, sizeof(address));
        address.sin6_len = sizeof(address);
        address.sin6_family = AF_INET6;
    #else
        struct sockaddr_in address;
        bzero(&address, sizeof(address));
        address.sin_len = sizeof(address);
        address.sin_family = AF_INET;
    #endif
        return [self managerForAddress:&address];
    }
    
    • 开启监听
    - (void)startMonitoring {
        [self stopMonitoring];
    
        if (!self.networkReachability) {
            return;
        }
    
         // 设置网络状态监听回调
        __weak __typeof(self)weakSelf = self;
        AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            strongSelf.networkReachabilityStatus = status;
            if (strongSelf.networkReachabilityStatusBlock) {
                strongSelf.networkReachabilityStatusBlock(status);
            }
        };
    
        SCNetworkReachabilityContext context = {0, (__bridge void *)callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL};
        SCNetworkReachabilitySetCallback(self.networkReachability, AFNetworkReachabilityCallback, &context);
        SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
            SCNetworkReachabilityFlags flags;
            if (SCNetworkReachabilityGetFlags(self.networkReachability, &flags)) {
                AFPostReachabilityStatusChange(flags, callback);
            }
        });
    }
    
    • 关闭监听
    - (void)stopMonitoring {
        if (!self.networkReachability) {
            return;
        }
    
        SCNetworkReachabilityUnscheduleFromRunLoop(self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
    }
    

    相关文章

      网友评论

          本文标题:iOS AFNetworking

          本文链接:https://www.haomeiwen.com/subject/qvbavxtx.html