151

我知道这个问题似乎是许多其他问题的欺骗,但是,我觉得这个简单的案例在这里没有得到很好的解释。来自 Android 和 BlackBerry 背景,HTTPUrlConnection如果没有可用的连接,通过请求立即失败。这似乎是完全理智的行为,我惊讶地发现NSURLConnectioniOS 没有模仿它。

我了解 Apple(以及扩展它的其他人)提供了一个Reachability类来帮助确定网络状态。我很高兴第一次看到这个并完全期望看到类似的东西bool isNetworkAvailable(),但令我惊讶的是,我发现一个复杂的系统需要通知注册和回调,以及一堆看似不必要的细节。肯定有更好的办法。

我的应用程序已经优雅地处理了连接失败,包括没有连接。用户收到失败通知,应用程序继续运行。

因此我的要求很简单:我可以在所有 HTTP 请求之前调用单个同步函数,以确定我是否应该打扰实际发送请求。理想情况下,它不需要设置,只返回一个布尔值。

这在iOS上真的不可能吗?

4

16 回答 16

253

我做了更多的研究,我正在用更新的解决方案更新我的答案。我不确定您是否已经看过它,但 Apple 提供了一个不错的示例代码。

在此处下载示例代码

在项目中包含 Reachability.h 和 Reachability.m 文件。查看 ReachabilityAppDelegate.m 以查看有关如何确定主机可达性、WiFi 可达性、WWAN 等的示例。对于网络可达性的非常简单的检查,您可以执行以下操作

Reachability *networkReachability = [Reachability reachabilityForInternetConnection];   
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];    
if (networkStatus == NotReachable) {        
    NSLog(@"There IS NO internet connection");        
} else {        
     NSLog(@"There IS internet connection");        
}

@BenjaminPiette's:不要忘记将 SystemConfiguration.framework 添加到您的项目中。

于 2012-01-11T02:06:25.173 回答
45

看到这个线程是这类问题的顶级谷歌结果,我想我会提供对我有用的解决方案。我已经在使用AFNetworking,但是直到我的项目进行到一半,搜索才揭示了如何使用 AFNetworking 完成这项任务。

你想要的是 AFNetworkingReachabilityManager

// -- Start monitoring network reachability (globally available) -- //
[[AFNetworkReachabilityManager sharedManager] startMonitoring];

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    NSLog(@"Reachability changed: %@", AFStringFromNetworkReachabilityStatus(status));


    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            // -- Reachable -- //
            NSLog(@"Reachable");
            break;
        case AFNetworkReachabilityStatusNotReachable:
        default:
            // -- Not reachable -- //
            NSLog(@"Not Reachable");
            break;
    }

}];

您还可以使用以下内容同步测试可达性(一旦开始监控):

-(BOOL) isInternetReachable
{
    return [AFNetworkReachabilityManager sharedManager].reachable;
}
于 2014-03-29T22:37:02.340 回答
40

很抱歉回复得太晚了,但我希望这个答案可以帮助将来的人。

以下是一个小的原生 C 代码片段,无需任何额外的类即可检查互联网连接。

添加以下标题:

#include<unistd.h>
#include<netdb.h>

代码:

-(BOOL)isNetworkAvailable
{
    char *hostname;
    struct hostent *hostinfo;
    hostname = "google.com";
    hostinfo = gethostbyname (hostname);
    if (hostinfo == NULL){
        NSLog(@"-> no connection!\n");
        return NO;
    }
    else{
        NSLog(@"-> connection established!\n");
        return YES;
    }
}

斯威夫特 3

func isConnectedToInternet() -> Bool {
    let hostname = "google.com"
    //let hostinfo = gethostbyname(hostname)
    let hostinfo = gethostbyname2(hostname, AF_INET6)//AF_INET6
    if hostinfo != nil {
        return true // internet available
      }
     return false // no internet
    }
于 2013-02-14T07:58:34.473 回答
31

我目前使用这种简单的同步方法,它不需要您的项目或委托中的额外文件。

进口:

#import <SystemConfiguration/SCNetworkReachability.h>

创建此方法:

+(bool)isNetworkAvailable
{
    SCNetworkReachabilityFlags flags;
    SCNetworkReachabilityRef address;
    address = SCNetworkReachabilityCreateWithName(NULL, "www.apple.com" );
    Boolean success = SCNetworkReachabilityGetFlags(address, &flags);
    CFRelease(address);

    bool canReach = success
                    && !(flags & kSCNetworkReachabilityFlagsConnectionRequired)
                    && (flags & kSCNetworkReachabilityFlagsReachable);

    return canReach;
}

然后,如果你把它放在一个MyNetworkClass

if( [MyNetworkClass isNetworkAvailable] )
{
   // do something networky.
}

如果您在模拟器中进行测试,请打开和关闭 Mac 的 wifi,因为模拟器似乎会忽略手机设置。

更新:

  1. 最后我使用了线程/异步回调来避免阻塞主线程;并定期重新测试,以便我可以使用缓存的结果 - 尽管您应该避免不必要地保持数据连接打开。

  2. 正如@thunk 所描述的,有更好的 URL 可供使用,Apple 自己也使用这些 URL。http://cadinc.com/blog/why-your-apple-ios-7-device-wont-connect-to-the-wifi-network

于 2014-10-27T17:52:19.817 回答
11

这是可能的,如果您在完成实现时查看它,这真的很简单,这又是 - 非常简单,因为您需要的唯一项目是两个布尔变量:互联网可达性和主机可达性(您通常需要不止一个)。一旦组装了可以确定连接状态的帮助程序类,您就真的不再关心了解这些过程所需的实现了。

例子:

#import <Foundation/Foundation.h>

@class Reachability;

@interface ConnectionManager : NSObject {
    Reachability *internetReachable;
    Reachability *hostReachable;
}

@property BOOL internetActive;
@property BOOL hostActive;

- (void) checkNetworkStatus:(NSNotification *)notice;

@end

和 .m 文件:

#import "ConnectionManager.h"
#import "Reachability.h"

@implementation ConnectionManager
@synthesize internetActive, hostActive;

-(id)init {
    self = [super init];
    if(self) {

    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    hostReachable = [[Reachability reachabilityWithHostName:@"www.apple.com"] retain];
    [hostReachable startNotifier];

    return self;
}

- (void) checkNetworkStatus:(NSNotification *)notice {
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)

    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            self.internetActive = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            self.internetActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            self.internetActive = YES;

            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
            self.hostActive = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
            self.hostActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            self.hostActive = YES;

            break;

        }
    }

}

// If lower than SDK 5 : Otherwise, remove the observer as pleased.

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

@end
于 2012-01-11T00:21:25.750 回答
6

以前有人以一种简单、可重用的方式解决了这个问题。DDGReachability.

编辑:或者tonymillion/Reachability

于 2012-01-11T02:09:53.140 回答
4

我提取了代码并将其放入一个方法中,希望对其他人有所帮助。

#import <SystemConfiguration/SystemConfiguration.h>

#import <netinet/in.h>
#import <netinet6/in6.h>

...

- (BOOL)isInternetReachable
{    
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress);
    SCNetworkReachabilityFlags flags;

    if(reachability == NULL)
        return false;

    if (!(SCNetworkReachabilityGetFlags(reachability, &flags)))
        return false;

    if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
        // if target host is not reachable
        return false;


    BOOL isReachable = false;


    if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
    {
        // if target host is reachable and no connection is required
        //  then we'll assume (for now) that your on Wi-Fi
        isReachable = true;
    }


    if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
         (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
    {
        // ... and the connection is on-demand (or on-traffic) if the
        //     calling application is using the CFSocketStream or higher APIs

        if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
        {
            // ... and no [user] intervention is needed
            isReachable = true;
        }
    }

    if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
    {
        // ... but WWAN connections are OK if the calling application
        //     is using the CFNetwork (CFSocketStream?) APIs.
        isReachable = true;
    }


    return isReachable;


}
于 2012-12-20T10:17:25.443 回答
4

我在这里写了接受答案的快速版本,如果有人发现它有用,代码写成 swift 2,

您可以从SampleCode下载所需的文件

添加Reachability.hReachability.m归档到您的项目中,

Bridging-Header.h现在,如果您的项目不存在文件,则需要创建文件,

在您的Bridging-Header.h文件中添加以下行:

#import "Reachability.h"

现在为了检查互联网连接

static func isInternetAvailable() -> Bool {
    let networkReachability : Reachability = Reachability.reachabilityForInternetConnection()
    let networkStatus : NetworkStatus = networkReachability.currentReachabilityStatus()

    if networkStatus == NotReachable {
        print("No Internet")
        return false
    } else {
        print("Internet Available")
        return true
    }

}
于 2015-12-14T06:50:44.547 回答
3

我认为这可能会有所帮助..

[[AFNetworkReachabilityManager sharedManager] startMonitoring];

if([AFNetworkReachabilityManager sharedManager].isReachable)
{
    NSLog(@"Network reachable");
}
else
{   
   NSLog(@"Network not reachable");
}
于 2015-10-20T07:06:05.010 回答
3

如果你已经在项目中配置了 AFNetworking,你也可以试试这个。

-(void)viewDidLoad{  // -- add connectivity notification --//
[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(ReachabilityDidChangeNotification:) name:AFNetworkingReachabilityDidChangeNotification object:nil];}
-(void)ReachabilityDidChangeNotification:(NSNotification *)notify
{
// -- NSLog(@"Reachability changed: %@", AFStringFromNetworkReachabilityStatus(status));  -- //
NSDictionary *userInfo =[notif userInfo];
AFNetworkReachabilityStatus status= [[userInfo valueForKey:AFNetworkingReachabilityNotificationStatusItem] intValue];
switch (status)
{
    case AFNetworkReachabilityStatusReachableViaWWAN:
    case AFNetworkReachabilityStatusReachableViaWiFi:
        // -- Reachable -- //
// -- Do your stuff when internet connection is available -- //
        [self getLatestStuff];
        NSLog(@"Reachable");
        break;
    case AFNetworkReachabilityStatusNotReachable:
    default:
        // -- Not reachable -- //
        // -- Do your stuff for internet connection not available -- //
NSLog(@"Not Reachable");
        break;
}
}
于 2017-08-30T08:59:26.277 回答
1

这是使用 Swift 检查连接性的一个很好的解决方案,而不使用 Reachability。我在这个博客上找到了它。

在您的项目中创建一个名为Network.swift(例如)的新 Swift 文件。将此代码粘贴到该文件中:

import Foundation

public class Network {

    class func isConnectedToNetwork()->Bool{

        var Status:Bool = false
        let url = NSURL(string: "http://google.com/")
        let request = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = "HEAD"
        request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
        request.timeoutInterval = 10.0

        var response: NSURLResponse?

        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?

        if let httpResponse = response as? NSHTTPURLResponse {
            if httpResponse.statusCode == 200 {
                Status = true
            }
        }

        return Status
    }
}

然后,您可以使用以下方法检查项目中任何位置的连接性:

if Network.isConnectedToNetwork() == true {
    println("Internet connection OK")
} else {
    println("Internet connection FAILED")
}
于 2015-05-24T17:23:08.537 回答
0

编辑:这不适用于网络 URL(见评论)

从 iOS 5 开始,有一个新的 NSURL 实例方法:

- (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error

将其指向您关心的网站或指向apple.com;我认为这是一个新的单线电话,可以查看互联网是否在您的设备上运行。

于 2012-01-28T15:54:08.420 回答
0

我也对可用的 Internet 检查选项不满意(为什么这不是本机 API?!?!)

我自己的问题是 100% 的数据包丢失——当设备连接到路由器时,但路由器没有连接到 Internet。可达性和其他将挂起很长时间。我创建了一个实用程序单例类来通过添加异步超时来处理这个问题。它在我的应用程序中运行良好。希望能帮助到你。这是github上的链接:

https://github.com/fareast555/TFInternetChecker

于 2015-08-22T16:53:41.907 回答
0

检查 (iOS) Xcode 8.2、Swift 3.0 中的 Internet 连接可用性

这是检查网络可用性的简单方法。我设法将它翻译成 Swift 2.0,这里是最终代码。现有的 Apple Reachability 类和其他第三方库似乎太复杂而无法转换为 Swift。

这适用于 3G 和 WiFi 连接。

不要忘记将“SystemConfiguration.framework”添加到您的项目构建器中。

//Create new swift class file Reachability in your project.

import SystemConfiguration

public class Reachability {
class func isConnectedToNetwork() -> Bool {
    var zeroAddress = sockaddr_in()
    zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)
    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
        $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
            SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
        }
    }
    var flags = SCNetworkReachabilityFlags()
    if !SCNetworkReachabilityGetFlags(defaultRouteReachability! , &flags) {
        return false
    }
    let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
    let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
    return (isReachable && !needsConnection)
   }
}

// Check network connectivity from anywhere in project by using this code.

if Reachability.isConnectedToNetwork() == true {
     print("Internet connection OK")
} else {
 print("Internet connection FAILED")
}
于 2015-12-17T11:46:15.247 回答
0

受 tonymillion 的启发,用闭包替换了用 Swift重写的 Apple 的可达性: https ://github.com/ashleymills/Reachability.swift

  1. 将文件拖放Reachability.swift到您的项目中。或者,使用CocoaPodsCarthage - 请参阅项目自述文件的安装部分。

  2. 获取有关网络连接的通知:

    //declare this property where it won't go out of scope relative to your listener
    let reachability = Reachability()!
    
    reachability.whenReachable = { reachability in
        if reachability.isReachableViaWiFi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
    
    reachability.whenUnreachable = { _ in
        print("Not reachable")
    }
    
    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
    

    并用于停止通知

    reachability.stopNotifier()
    
于 2017-10-09T13:27:26.093 回答
0

阿拉莫菲尔

如果您已经将Alamofire用于所有 RESTful Api,那么您可以从中受益。

您可以将以下类添加到您的应用程序中,并调用MNNetworkUtils.main.isConnected()以获取有关其是否已连接的布尔值。

#import Alamofire

class MNNetworkUtils {
  static let main = MNNetworkUtils()
  init() {
    manager = NetworkReachabilityManager(host: "google.com")
    listenForReachability()
  }

  private let manager: NetworkReachabilityManager?
  private var reachable: Bool = false
  private func listenForReachability() {
    self.manager?.listener = { [unowned self] status in
      switch status {
      case .notReachable:
        self.reachable = false
      case .reachable(_), .unknown:
        self.reachable = true
      }
    }
    self.manager?.startListening()
  }

  func isConnected() -> Bool {
    return reachable
  }
}

这是一个单例类。每次,当用户连接或断开网络时,它都会self.reachable正确覆盖为真/假,因为我们开始监听NetworkReachabilityManager单例初始化。

此外,为了监控可达性,您需要提供一个主机,目前我正在使用google.com随时更改为任何其他主机或您的主机(如果需要)。

于 2018-04-02T17:07:39.123 回答