码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-05-08 16:35:26      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:wifi   下载   网络   开发   应用   

检测网络状态

在网络应用中,需要对用户设备的网络状态进行实时监控,目的是
让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
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];
    }
}

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

标签:wifi   下载   网络   开发   应用   

原文地址:http://blog.csdn.net/wangzi11322/article/details/45580917

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!