码迷,mamicode.com
首页 > 其他好文 > 详细

判断网络

时间:2015-04-01 13:28:15      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:网络

#import "ViewController.h"

#import "Reachability.h"

#import "Common.h"


@interface ViewController ()

{

    NetworkStatus netstatus;

    BOOL isConnected;

    Reachability * hostReach;

}



@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)butonClick:(id)sender {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];

    [hostReach startNotifier];

}

// 网络监听事件

-(void)reachabilityChanged:(NSNotification *)note

{

    NSString * connectionKind = nil;

    Reachability * curReach = [note object];

    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);

    netstatus = [curReach currentReachabilityStatus];

    switch (netstatus)

    {

        case NotReachable:

            connectionKind = @"当前没有网络链接\n请检查你的网络设置";

            NSLog(@"%@",connectionKind);

            isConnected =NO;

            break;

        case ReachableViaWiFi:

            connectionKind = @"当前使用的网络类型是WIFI";

            NSLog(@"%@",connectionKind);


            isConnected =YES;

            break;

        case ReachableViaWWAN:

            connectionKind = @"您现在使用的是2G/3G网络\n可能会产生流量费用";

            NSLog(@"%@",connectionKind);


            isConnected =YES;

            break;

        default:

            break;

    }

    

    if (isConnected == NO)

    {

        [Common showAlert:connectionKind];

    }

}

demo链接        http://download.csdn.net/my

判断网络

标签:网络

原文地址:http://blog.csdn.net/tubiebutu/article/details/44804435

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