Iphone 在iOS中检查Internet连接的最简单方法是什么?
可能重复:Iphone 在iOS中检查Internet连接的最简单方法是什么?,iphone,ios,objective-c,Iphone,Ios,Objective C,可能重复: 我正在寻找最快最简单的方法来检查iOS中的连接 我发现: -(BOOL)connectedToNetwork { NSURL* url = [[NSURL alloc] initWithString:@"http://google.com/"]; NSData* data = [NSData dataWithContentsOfURL:url]; if (data != nil) return YES; return NO; }
我正在寻找最快最简单的方法来检查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,我只是在搜索网站的是/否范围)看看苹果提供的解决方案
您的方法可能存在的问题是,您可能有一个超时,因此,同步下载某些数据可能会阻止您的应用程序。因此,苹果可能会拒绝您的应用程序
可按如下方式使用:
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
}
我建议不要采用这种方法。因为这个代码,我的一个应用程序被拒绝了。相反,你应该去 正如@who9vy所说的使用 将两个类Reachability.h和Reachability.m导入到项目中 使用方法检查Internet连接
- (BOOL)isConnectedToInternet
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
检查可达性的最佳方法是Apple Recability类 检查这个
希望它能帮助您..使用此代码检查设备是否连接到internet 在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);
}
不建议检查与网站的Internet连接。因为即使是谷歌。在未来,谷歌网站可以单独关闭(谁知道呢),但你们将有互联网连接。所以,使用可恢复代码检查互联网连接。这是苹果公司推荐的。检查我的答案。是的,我们不应该使用“www.apple.com”,我们应该使用各自的主机名。如果apple.com无法访问,这并不意味着我们的应用程序无法访问我们的服务。只有在可用的情况下,我们才应该检查主机名。
-(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);
}