Check for Internet connection using the iOS SDK(使用 iOS SDK 检查 Internet 连接)
问题描述
使用 iOS SDK 检查互联网连接的最佳方法是什么?
Which is the best way to check the Internet connection using the iOS SDK?
推荐答案
最好的方法是使用可达性代码.查看此处获取苹果示例代码.它有很多方便的方法来检查互联网可用性、Wifi/WAN 连接检查等.
Best way is to use Reachability code. Check here for apple sample code. That has a lot of convenience methods to check internet availability, Wifi/WAN connectivity check etc..
例如:-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kReachabilityChangedNotification object:nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
- (void)networkChanged:(NSNotification *)notification
{
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) { NSLog(@"not reachable");}
else if (remoteHostStatus == ReachableViaWiFiNetwork) { NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { NSLog(@"carrier"); }
}
这篇关于使用 iOS SDK 检查 Internet 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!