首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Reachablity检测网络状态 包孕代码

2013-07-09 
Reachablity检测网络状态 包含代码检测网络状态的代码,使用block的形式:Reachability * reach [Reachabi

Reachablity检测网络状态 包含代码

检测网络状态的代码,使用block的形式:

    Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];    reach.reachableBlock = ^(Reachability * reachability)    {        dispatch_async(dispatch_get_main_queue(), ^{            blockLabel.text = @"Block Says Reachable";        });    };    reach.unreachableBlock = ^(Reachability * reachability)    {        dispatch_async(dispatch_get_main_queue(), ^{            blockLabel.text = @"Block Says Unreachable";        });    };    [reach startNotifier];

?

使用通知的形式:

- (void)viewDidLoad{    [[NSNotificationCenter defaultCenter] addObserver:self                                              selector:@selector(reachabilityChanged:)                                                  name:kReachabilityChangedNotification                                                object:nil];}-(void)reachabilityChanged:(NSNotification*)note{    Reachability * reach = [note object];        if([reach isReachable])    {        notificationLabel.text = @"Notification Says Reachable";    }    else    {        notificationLabel.text = @"Notification Says Unreachable";    }}

?

热点排行