iOS开发 - 检测网络状态(WIFI、2G/3G/4G)

检测网络状态

在网络应用中,需要对用户设备的网络状态进行实时监控,目的是
让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
WIFI\3G网络:自动下载高清图片
低速网络:只下载缩略图
没有网络:只显示离线的缓存数据

苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

Reachability的使用步骤
添加框架SystemConfiguration.framework
技术分享
添加源代码
技术分享
包含头文件

#import "Reachability.h"

抽取工具类

常见用法
// 是否WIFI
+ (BOOL) IsEnableWIFI {
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}

// 是否3G
+ (BOOL) IsEnable3G {
    return ([[Reachability reachabilityForInternetConnectionen] currentReachabilityStatus] != NotReachable);
}

网络监控

[[NSNotificationCenter defaultCenter] addObserver:self
 selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
self.netReachability = [Reachability reachabilityForInternetConnection];
[self.netReachability startNotifier];

- (void)dealloc
{
    [self.netReachability stopNotifier];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}

检测网络状态实例

NetWorkTool工具类

#import <Foundation/Foundation.h>

@interface NetworkTool : NSObject
/**
 *  是否WIFI
 */
+ (BOOL)isEnableWIFI;

/**
 *  是否3G
 */
+ (BOOL)isEnable3G;
@end
#import "NetWorkTool.h"
#import "Reachability.h"

@implementation NetworkTool
// 是否WIFI
+ (BOOL)isEnableWIFI {
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}

// 是否3G
+ (BOOL)isEnable3G {
    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
@end

实现类

#import "ViewController.h"
#import "Reachability.h"
#import "NetworkTool.h"

@interface ViewController ()
@property (nonatomic, strong) Reachability *reachability;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 监听网络状态发生改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];

    // 获得Reachability对象
    self.reachability = [Reachability reachabilityForInternetConnection];
    // 开始监控网络
    [self.reachability startNotifier];

}

- (void)dealloc
{
    [self.reachability stopNotifier];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)networkStateChange
{
    NSLog(@"网络状态改变了");
    [self checkNetworkState];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self checkNetworkState];
}
#pragma mark - App应用程序网络类型改变就会调用
- (void)networkStateChange
{
    if ([NetWorkTool isEnableWIFI]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示您" message:@"您现在网络环境为wifi!开始畅享吧!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else if ([NetWorkTool isEnable3G]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示您"  message:@"您现在网络环境为3G/4G网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示您" message:@"您当前没有可连的网络或已经掉线!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。