我正在寻找检查iOS连接的最快最简单的方法。
我发现了这个:
-(BOOL)connectedToNetwork {
NSURL* url = [[NSURL alloc] initWithString:@"http://google.com/"];
NSData* data = [NSData dataWithContentsOfURL:url];
if (data != nil)
return YES;
return NO;
}
你知道是否有更简单的东西???
伙计们,感谢所有答案,但我正在寻找最简单,最轻便的解决方案,而不是最好的解决方案(即不需要区分3G / Wi-Fi,我只是在搜索网站的是/否覆盖率
答案 0 :(得分:4)
查看Apple提供的Reachability Example。
您的方法可能存在的问题是您可能会超时,因此,某些数据的同步下载可能会阻止您的应用。因此,Apple可能拒绝您的应用。
Reachability Example可以按如下方式使用:
Reachability *_reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus remoteHostStatus = [_reachability currentReachabilityStatus];
if (remoteHostStatus == NotReachable) {
// not reachable
} else if (remoteHostStatus == ReachableViaWiFi) {
// reachable via Wifi
} else if (remoteHostStatus == ReachableViaWWAN) {
// reachable via WWAN
}
答案 1 :(得分:2)
我建议不要采用这种方法。由于此代码,我面临拒绝我的一个应用程序。而是使用Apple's Reachability Classes.
答案 2 :(得分:2)
使用此代码检查设备是否已连接到互联网
在viewDidLoad中使用此代码:
Reachability* internetReachable; = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ;
[hostReachable startNotifier];
并将此函数添加到您的代码中:
-(void) checkNetworkStatus:(NSNotification *)notice
{
recheabilityBool=FALSE;
nonrecheabilityBool=FALSE;
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
nonrecheabilityBool=TRUE;
internetCon=0;
//NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
internetCon=404;
[prefs setInteger:internetCon forKey:@"conKey"];
//NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
//NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
internetCon=0;
if( nonrecheabilityBool==FALSE)
{
//NSLog(@"A gateway to the host server is down.");
}
break;
}
case ReachableViaWiFi:
{
if(recheabilityBool==FALSE)
{
recheabilityBool=TRUE;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
internetCon=404;
[prefs setInteger:internetCon forKey:@"conKey"];
//NSLog(@"The internet is working via WIFI.");
break;
}
//NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
//NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
答案 3 :(得分:1)
Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:@"www.example.com"]; // set your host name here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
How to check for an active Internet connection on iOS or OSX?
答案 4 :(得分:0)
正如@ who9vy所说使用Reachability Example
将两个类Reachability.h和Reachability.m导入项目
使用方法检查Internet连接
- (BOOL)isConnectedToInternet
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
答案 5 :(得分:0)
检查可达性的最佳方法是Apple Reachability类检查link
希望它可以帮助你..