美文网首页
Reachability类的学习(二)

Reachability类的学习(二)

作者: ashura_ | 来源:发表于2017-04-27 19:48 被阅读0次

为了避免由于使用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];

相关文章

网友评论

      本文标题:Reachability类的学习(二)

      本文链接:https://www.haomeiwen.com/subject/fnynzttx.html