Reachability类的学习(二)

2017-04-27  本文已影响0人  ashura_

为了避免由于使用SCNetworkReachabilityGetFlags在主线程里会引起卡死的问题,采用一个GCD相关的APISCNetworkReachabilitySetDispatchQueue来检测当前的网络状况

官网

这里

用法

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
    // keep in mind this is called on a background thread
    // and if you are updating the UI it needs to happen
    // on the main thread, like this:

    dispatch_async(dispatch_get_main_queue(), ^{
      NSLog(@"REACHABLE!");
    });
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"UNREACHABLE!");
};

// Start the notifier, which will cause the reachability object to retain itself!
[reach startNotifier];
上一篇 下一篇

猜你喜欢

热点阅读