网络状态的监测

        首先创建一个空的工程,导入类Reachability.h和Reachability.m,当然,千万不要忘记添加SystemConfiguration.framework这个框架,不然,你的程序运行时将会发生很多错误。

下面直接看代码:AppDelegate.h中需要添加几行这样的代码

#import <UIKit/UIKit.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"

@interface YueAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (retain, nonatomic) Reachability *hostReach;

@end

AppDelegate.m中的代码就复杂一些,下面是代码:
#import "BoAppDelegate.h"
#import "Reachability.h"

@implementation BoAppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    //将selector方法添加到消息中心,实现全局监测
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reachablityChanged:) name:kReachabilityChangedNotification object:nil];
    //初始化
    self.hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"]retain];
    //开始监测
    [self.hostReach startNotifier];
    
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)reachablityChanged:(NSNotification *)note
{
    Reachability *pReachablith = [note object];
    //断言语句(实现容错保护)
    NSParameterAssert([pReachablith isKindOfClass:[Reachability class]]);
    NSString *pStr_3G = @"当前网络为2G或者3G";
    NSString *pStr_WIFI = @"当前的网络为WIFI";
    NSString *pStr_No = @"无网络,请确认连接状态";    
    switch ([pReachablith currentReachabilityStatus])
    {
        case NotReachable:
            [self alertShow:pStr_No];
            break;
        case ReachableViaWiFi:
            [self alertShow:pStr_WIFI];
            break;
        case ReachableViaWWAN:
            [self alertShow:pStr_3G];
            break;
        default:
            [self alertShow:@"error"];
            break;
    }
}
- (void)alertShow:(NSString *)mes
{
    UIAlertView *pAlertView  = [[UIAlertView alloc]initWithTitle:@"通知" message:mes delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
    [pAlertView show];
    [pAlertView release];
}



- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值