在 objective c 中检查网络可达性的最佳方法是什么
Which is the best method to check the network reachability in objective c
我是 objective c 的新手。我发现了很多检查互联网连接的方法,比如可达性测试,使用 AF Networking etc.Which 是最好的方法?有什么帮助吗?
我总是喜欢使用 Reachability https://github.com/tonymillion/Reachability,如下所示。 GitHub 上的描述非常清楚为什么要使用它 -> "This is a drop-in replacement for Apple's Reachability class. It is ARC-compatible, and it uses the new GCD methods to notify of network interface changes".
Reachability *internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
//do something
NSLog(@"REACHABLE!");
} error:^(NSError * _Nullable error) {
NSLog(@"Error:%@ ", [error localizedDescription]);
}];
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
//do something
NSLog(@"UNREACHABLE!");
});
};
[internetReachableFoo startNotifier];
祝你好运:)
我是 objective c 的新手。我发现了很多检查互联网连接的方法,比如可达性测试,使用 AF Networking etc.Which 是最好的方法?有什么帮助吗?
我总是喜欢使用 Reachability https://github.com/tonymillion/Reachability,如下所示。 GitHub 上的描述非常清楚为什么要使用它 -> "This is a drop-in replacement for Apple's Reachability class. It is ARC-compatible, and it uses the new GCD methods to notify of network interface changes".
Reachability *internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
//do something
NSLog(@"REACHABLE!");
} error:^(NSError * _Nullable error) {
NSLog(@"Error:%@ ", [error localizedDescription]);
}];
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
//do something
NSLog(@"UNREACHABLE!");
});
};
[internetReachableFoo startNotifier];
祝你好运:)