我想检查一下我是否可以在iOS上使用Cocoa Touch库或在macOS上使用Cocoa库连接互联网。

我想出了一个方法来做这个使用NSURL。我的方法似乎有点不可靠(因为即使谷歌也可能有一天宕机,依赖第三方似乎很糟糕),虽然如果谷歌没有响应,我可以检查其他一些网站的响应,但这似乎是浪费和不必要的应用程序开销。

- (BOOL)connectedToInternet {
    NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
    return ( URLString != NULL ) ? YES : NO;
}

是我做得不好,(更不用说stringWithContentsOfURL已弃用在iOS 3.0和macOS 10.4),如果是这样,有什么更好的方法来实现这一点?


苹果提供了一个示例应用程序,它正是这样做的:

可达性


Apple提供了示例代码来检查不同类型的网络可用性。在iPhone开发者的烹饪书中也有一个例子。

注:请参阅@KHG关于使用苹果可达代码的回答的评论。


我在这个讨论中使用了代码,它似乎工作得很好(阅读整个帖子!)。

我还没有对每一种可能的连接(比如临时Wi-Fi)进行详尽的测试。


这曾经是正确的答案,但现在已经过时了,因为你应该订阅通知以获得可访问性。这个方法同步检查:


你可以使用苹果的Reachability类。它还可以让你检查是否启用了Wi-Fi:

Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:@"www.example.com"];    // Set your host name here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];

if (remoteHostStatus == NotReachable) { }
else if (remoteHostStatus == ReachableViaWiFiNetwork) { }
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { }

Reachability类并没有随SDK一起发布,而是作为这个Apple示例应用程序的一部分。只需下载它,并复制Reachability.h/m到您的项目。另外,您必须将SystemConfiguration框架添加到您的项目中。


只有Reachability类已经更新。你现在可以使用:

Reachability* reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

if (remoteHostStatus == NotReachable) { NSLog(@"not reachable");}
else if (remoteHostStatus == ReachableViaWWAN) { NSLog(@"reachable via wwan");}
else if (remoteHostStatus == ReachableViaWiFi) { NSLog(@"reachable via wifi");}

重要:该检查应该始终异步执行。下面的大多数答案是同步的,所以要小心,否则你会冻结你的应用程序。


斯威夫特

通过CocoaPods或Carthage安装:https://github.com/ashleymills/Reachability.swift 通过闭包测试可达性 let reachability =可达性()! 可达性。whenReachable ={可达性 如果可达性。连接== .wifi { 打印(“可通过WiFi访问”) }其他{ 打印(“可通过蜂窝连接”) } } 可达性。whenUnreachable = {_ in 打印(“不可以”) } {做 尝试reachability.startNotifier () } catch { 打印("无法启动通知程序") }


objective - c

将SystemConfiguration框架添加到项目中,但不要担心将它包含在任何地方 添加Tony Million版本的Reachability.h和Reachability。m到项目(在这里找到:https://github.com/tonymillion/Reachability) 更新接口部分 #进口“Reachability.h” //在视图控制器的.m文件中添加这个接口 @interface MyViewController () { 可达性* internetReachableFoo; } @end 然后在视图控制器的。m文件中实现这个方法,你可以调用它 //检查我们是否有网络连接 - (void) testInternetConnection { internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; //网络可达 internetReachableFoo。reachableBlock = ^(可达性*reach) { //更新主线程的UI 设置(dispatch_get_main_queue()、^ { NSLog(@“Yayyy,我们有了互联网!”); }); }; //网络不可达 internetReachableFoo。unreachableBlock = ^(可达性*reach) { //更新主线程的UI 设置(dispatch_get_main_queue()、^ { NSLog(@"有人破坏了互联网:("); }); }; [internetReachableFoo startNotifier]; }

重要提示:Reachability类是项目中最常用的类之一,因此您可能会遇到与其他项目的命名冲突。如果发生这种情况,您将不得不重命名Reachability.h和Reachability对中的一个。M文件到其他文件来解决这个问题。

注意:您使用的域名并不重要。这只是在测试通往任何领域的入口。


我喜欢把事情简单化。我的做法是:

//Class.h
#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>

- (BOOL)connected;

//Class.m
- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return networkStatus != NotReachable;
}

然后,每当我想要查看是否有连接时,我就使用这个:

if (![self connected]) {
    // Not connected
} else {
    // Connected. Do some Internet stuff
}

这个方法不会等待网络状态的改变来执行操作。它只在您要求时测试状态。


使用Apple的Reachability代码,我创建了一个函数,可以正确地检查这一点,而无需包含任何类。

在你的项目中包含SystemConfiguration.framework。

进行一些导入:

#import <sys/socket.h>
#import <netinet/in.h>
#import <SystemConfiguration/SystemConfiguration.h>

现在只需调用这个函数:

/*
Connectivity testing code pulled from Apple's Reachability Example: https://developer.apple.com/library/content/samplecode/Reachability
 */
+(BOOL)hasConnectivity {
    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);
    if (reachability != NULL) {
        //NetworkStatus retVal = NotReachable;
        SCNetworkReachabilityFlags flags;
        if (SCNetworkReachabilityGetFlags(reachability, &flags)) {
            if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
            {
                // If target host is not reachable
                return NO;
            }

            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
                return YES;
            }


            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
                    return YES;
                }
            }

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

    return NO;
}

这是为你测试的iOS 5。


关于Reachability的iOS 5版本是darkseed/Reachability.h。不是我的!=)


这里有一个漂亮的,使用ARC和gcd的现代化可达性:

可达性


您可以使用Reachability by(可在此处查看)。

#import "Reachability.h"

- (BOOL)networkConnection {
    return [[Reachability reachabilityWithHostName:@"www.google.com"] currentReachabilityStatus];
}

if ([self networkConnection] == NotReachable) { /* No Network */ } else { /* Network */ } //Use ReachableViaWiFi / ReachableViaWWAN to get the type of connection.

这里有一个非常简单的答案:

NSURL *scriptUrl = [NSURL URLWithString:@"http://www.google.com/m"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data)
    NSLog(@"Device is connected to the Internet");
else
    NSLog(@"Device is not connected to the Internet");

URL应该指向一个非常小的网站。我在这里使用谷歌的移动网站,但如果我有一个可靠的网络服务器,我会上传一个只有一个字符的小文件,以获得最快的速度。

如果检查设备是否以某种方式连接到互联网是你想做的一切,我绝对推荐使用这个简单的解决方案。如果您需要知道用户是如何连接的,那么使用可达性是正确的方法。

小心:这将短暂地阻塞你的线程,而它加载网站。对我来说,这不是问题,但你应该考虑一下(感谢Brad指出了这一点)。


- (void)viewWillAppear:(BOOL)animated
{
    NSString *URL = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];

    return (URL != NULL ) ? YES : NO;
}

或者使用Reachability类。

使用iPhone SDK有两种方法来检查网络可用性:

1. 检查“谷歌”页面是否打开。

2. 可达性类

有关更多信息,请参考Reachability (Apple Developer)。


以下是我在我的应用程序中是如何做到的:虽然200状态响应代码不能保证任何东西,但对我来说已经足够稳定了。这并不需要像NSData回答一样多的加载,因为我的只是检查HEAD响应。

银行的国际代码

func checkInternet(flag:Bool, completionHandler:(internet:Bool) -> Void)
{
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    let url = NSURL(string: "http://www.google.com/")
    let request = NSMutableURLRequest(URL: url!)

    request.HTTPMethod = "HEAD"
    request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
    request.timeoutInterval = 10.0

    NSURLConnection.sendAsynchronousRequest(request, queue:NSOperationQueue.mainQueue(), completionHandler:
    {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

        UIApplication.sharedApplication().networkActivityIndicatorVisible = false

        let rsp = response as! NSHTTPURLResponse?

        completionHandler(internet:rsp?.statusCode == 200)
    })
}

func yourMethod()
{
    self.checkInternet(false, completionHandler:
    {(internet:Bool) -> Void in

        if (internet)
        {
            // "Internet" aka Google URL reachable
        }
        else
        {
            // No "Internet" aka Google URL un-reachable
        }
    })
}

objective - c代码

typedef void(^connection)(BOOL);

- (void)checkInternet:(connection)block
{
    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
    NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:url];
    headRequest.HTTPMethod = @"HEAD";

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    defaultConfigObject.timeoutIntervalForResource = 10.0;
    defaultConfigObject.requestCachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];

    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:headRequest
        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        if (!error && response)
        {
            block([(NSHTTPURLResponse *)response statusCode] == 200);
        }
    }];
    [dataTask resume];
}

- (void)yourMethod
{
    [self checkInternet:^(BOOL internet)
    {
         if (internet)
         {
             // "Internet" aka Google URL reachable
         }
         else
         {
             // No "Internet" aka Google URL un-reachable
         }
    }];
}

如果你正在使用AFNetworking,你可以使用它自己的实现internet可达状态。

使用AFNetworking的最好方法是子类化AFHTTPClient类,并使用这个类来进行网络连接。

使用这种方法的优点之一是,当可达性状态发生变化时,您可以使用块来设置所需的行为。假设我已经创建了一个名为BKHTTPClient的AFHTTPClient的单例子类(如AFNetworking文档上的“子类注释”所述),我会做如下的事情:

BKHTTPClient *httpClient = [BKHTTPClient sharedClient];
[httpClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
{
    if (status == AFNetworkReachabilityStatusNotReachable) 
    {
    // Not reachable
    }
    else
    {
        // Reachable
    }
}];

您还可以使用AFNetworkReachabilityStatusReachableViaWWAN和AFNetworkReachabilityStatusReachableViaWiFi枚举来检查Wi-Fi或WLAN连接。


-(void)newtworkType {

 NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
NSNumber *dataNetworkItemView = nil;

for (id subview in subviews) {
    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
        dataNetworkItemView = subview;
        break;
    }
}


switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
    case 0:
        NSLog(@"No wifi or cellular");
        break;

    case 1:
        NSLog(@"2G");
        break;

    case 2:
        NSLog(@"3G");
        break;

    case 3:
        NSLog(@"4G");
        break;

    case 4:
        NSLog(@"LTE");
        break;

    case 5:
        NSLog(@"Wifi");
        break;


    default:
        break;
}
}

首先下载reachability类并放入reachability.h和reachability.h。m文件在你的Xcode。

最好的方法是创建一个通用的Functions类(NSObject),这样你就可以在任何类中使用它。这是检查网络连接可达性的两种方法:

+(BOOL) reachabiltyCheck
{
    NSLog(@"reachabiltyCheck");
    BOOL status =YES;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(reachabilityChanged:)
                                          name:kReachabilityChangedNotification
                                          object:nil];
    Reachability * reach = [Reachability reachabilityForInternetConnection];
    NSLog(@"status : %d",[reach currentReachabilityStatus]);
    if([reach currentReachabilityStatus]==0)
    {
        status = NO;
        NSLog(@"network not connected");
    }
    reach.reachableBlock = ^(Reachability * reachability)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    };
    reach.unreachableBlock = ^(Reachability * reachability)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    };
    [reach startNotifier];
    return status;
}

+(BOOL)reachabilityChanged:(NSNotification*)note
{
    BOOL status =YES;
    NSLog(@"reachabilityChanged");
    Reachability * reach = [note object];
    NetworkStatus netStatus = [reach currentReachabilityStatus];
    switch (netStatus)
    {
        case NotReachable:
            {
                status = NO;
                NSLog(@"Not Reachable");
            }
            break;

        default:
            {
                if (!isSyncingReportPulseFlag)
                {
                    status = YES;
                    isSyncingReportPulseFlag = TRUE;
                    [DatabaseHandler checkForFailedReportStatusAndReSync];
                }
            }
            break;
    }
    return status;
}

+ (BOOL) connectedToNetwork
{
    // Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    if (!didRetrieveFlags)
    {
        NSLog(@"Error. Could not recover network reachability flags");
        return NO;
    }
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
    NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
    NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
    return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}

现在您可以通过调用这个类方法来检查任何类中的网络连接。


Reachability类可以确定一个设备的互联网连接是否可用…

但在访问内网资源时:

使用可达性类ping内网服务器总是返回true。

因此,在这种情况下,一个快速的解决方案是创建一个名为pingme的web方法以及服务上的其他web方法。 pingme应该返回一些东西。

所以我写了下面关于普通函数的方法

-(BOOL)PingServiceServer
{
    NSURL *url=[NSURL URLWithString:@"http://www.serveraddress/service.asmx/Ping"];

    NSMutableURLRequest *urlReq=[NSMutableURLRequest requestWithURL:url];

    [urlReq setTimeoutInterval:10];

    NSURLResponse *response;

    NSError *error = nil;

    NSData *receivedData = [NSURLConnection sendSynchronousRequest:urlReq
                                                 returningResponse:&response
                                                             error:&error];
    NSLog(@"receivedData:%@",receivedData);

    if (receivedData !=nil)
    {
        return YES;
    }
    else
    {
        NSLog(@"Data is null");
        return NO;
    }
}

上面的方法对我来说非常有用,所以每当我试图向服务器发送一些数据时,我总是使用这个低超时URLRequest检查我的内部网资源的可达性。


使用http://huytd.github.io/datatify/。这比自己添加库和编写代码要容易得多。


从https://github.com/tonymillion/Reachability获取Reachabilty类,在你的项目中添加系统配置框架,在你的类中导入Reachability.h,并实现如下所示的自定义方法:

- (BOOL)isConnectedToInternet
{
    //return NO; // Force for offline testing
    Reachability *hostReach = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [hostReach currentReachabilityStatus];
    return !(netStatus == NotReachable);
}

在ViewController中导入Reachable.h类,并使用以下代码检查连通性:

#define hasInternetConnection [[Reachability reachabilityForInternetConnection] isReachable]
if (hasInternetConnection){
      // To-do block
}

还有另一种使用iPhone SDK检查互联网连接的方法。

尝试为网络连接实现以下代码。

#import <SystemConfiguration/SystemConfiguration.h>
#include <netdb.h>

/**
     Checking for network availability. It returns
     YES if the network is available.
*/
+ (BOOL) connectedToNetwork
{

    // Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability =
        SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;

    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);

    if (!didRetrieveFlags)
    {
        printf("Error. Could not recover network reachability flags\n");
        return NO;
    }

    BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
    BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);

    return (isReachable && !needsConnection) ? YES : NO;
}

自己做这件事非常简单。下面的方法是可行的。只是要确保不允许将主机名协议(如HTTP、HTTPS等)与名称一起传入。

-(BOOL)hasInternetConnection:(NSString*)urlAddress
{
    SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [urlAddress UTF8String]);
    SCNetworkReachabilityFlags flags;
    if (!SCNetworkReachabilityGetFlags(ref, &flags))
    {
        return NO;
    }
    return flags & kSCNetworkReachabilityFlagsReachable;
}

这是快速、简单和无痛的。


我发现使用SimplePingHelper库很简单。

示例代码:chrishulbert/SimplePingHelper (GitHub)


除了可达性之外,您还可以使用Simple Ping助手库。它工作得很好,很容易集成。


首先:在framework中添加CFNetwork.framework

代码:ViewController.m

#import "Reachability.h"

- (void)viewWillAppear:(BOOL)animated
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];

    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    {
        /// Create an alert if connection doesn't work
        UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"No Internet Connection"   message:NSLocalizedString(@"InternetMessage", nil)delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [myAlert show];
        [myAlert release];
    }
    else
    {
         NSLog(@"INTERNET IS CONNECT");
    }
}

很简单的…试试下面这些步骤:

步骤1:将SystemConfiguration框架添加到项目中。


步骤2:将以下代码导入到头文件中。

#import <SystemConfiguration/SystemConfiguration.h>

第三步:使用以下方法

类型1: - (BOOL) currentNetworkStatus { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; BOOL连接; BOOL与; Const char *host = "www.apple.com"; SCNetworkReachabilityRef reachabilityreachabilitycreatewithname (NULL, host); SCNetworkReachabilityFlags旗帜; connected = SCNetworkReachabilityGetFlags(可达性,&flags); isConnected = NO; isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired); CFRelease(可达性); 返回与; }


类型2: 导入头文件:# Import "Reachability.h" (保龄球)currentNetworkStatus { 可达性*可达性=[互联网连接的可达性]; NetworkStatus NetworkStatus =[可达性currentReachabilityStatus]; 返回networkStatus !=不可访问; }


第四步:使用方法:

- (void)CheckInternet
{
    BOOL network = [self currentNetworkStatus];
    if (network)
    {
        NSLog(@"Network Available");
    }
    else
    {
        NSLog(@"No Network Available");
    }
}

进口“Reachability.h”

-(BOOL)netStat
{
    Reachability *test = [Reachability reachabilityForInternetConnection];
    return [test isReachable];
}

我认为这是最好的答案。

“是”意味着有联系。“No”表示断开连接。

#import "Reachability.h"

 - (BOOL)canAccessInternet
{
    Reachability *IsReachable = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStats = [IsReachable currentReachabilityStatus];

    if (internetStats == NotReachable)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

下载Reachability文件,https://gist.github.com/darkseed/1182373 在framework中添加CFNetwork.framework和'SystemConfiguration.framework' 导入“Reachability.h”

首先:在framework中添加CFNetwork.framework

代码:ViewController.m

- (void)viewWillAppear:(BOOL)animated
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];

    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    {
        /// Create an alert if connection doesn't work
        UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"No Internet Connection"   message:NSLocalizedString(@"InternetMessage", nil)delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [myAlert show];
        [myAlert release];
    }
    else
    {
         NSLog(@"INTERNET IS CONNECT");
    }
}

创建一个AFNetworkReachabilityManager对象,并使用以下代码跟踪网络连通性

self.reachabilityManager = [AFNetworkReachabilityManager managerForDomain:@"yourDomain"];
[self.reachabilityManager startMonitoring];
[self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                break;
            case AFNetworkReachabilityStatusNotReachable:
                break;
            default:
                break;
        }
    }];

步骤1:在项目中添加Reachability类。 步骤2:导入Reachability类 步骤3:创建下面的函数 - (BOOL)checkNetConnection { self.internetReachability =[可达性reachabilityForInternetConnection]; [self.internetReachability startNotifier]; NetworkStatus netStatus = [self.internetReachability currentReachabilityStatus]; switch (netStatus) { 案例NotReachable: { 返回NO; } 案例ReachableViaWWAN: { 返回YES; } 案例ReachableViaWiFi: { 返回YES; } } } 步骤4:调用函数如下所示: 如果(![self checkNetConnection]) { [GlobalFunctions showAlert: @”“ 信息:@“请连接到互联网!” canBtntitle:零 otherBtnTitle: @“Ok”); 返回; } 其他的 { 日志。V(“internet is connected”,“ok”); }


检查Internet连接的可用性(iOS) Xcode 8, Swift 3.0

这是一个简单的方法来检查网络可用性,如我们的设备是否连接到任何网络。我已经成功地将其翻译为Swift 3.0,这里是最终的代码。现有的Apple Reachability类和其他第三方库似乎太复杂了,无法翻译到Swift中。 这适用于3G、4G和WiFi连接。 不要忘记在你的项目构建器中添加“SystemConfiguration.framework”。

//Create new swift class file Reachability in your project.
import SystemConfiguration
public class InternetReachability {

class func isConnectedToNetwork() -> Bool {
   var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
   zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
   zeroAddress.sin_family = sa_family_t(AF_INET)
   let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
          SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
   }
   var flags: SCNetworkReachabilityFlags = 0
   if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
          return false
   }
   let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
   let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

   return isReachable && !needsConnection
  }
}

// Check network connectivity from anywhere in project by using this code.
 if InternetReachability.isConnectedToNetwork() == true {
         print("Internet connection OK")
  } else {
         print("Internet connection FAILED")
  }

这是Swift 3.0和异步的。大多数答案是同步解决方案,这将阻塞你的主线程,如果你有一个非常慢的连接。

这个解决方案更好,但并不完美,因为它依赖于谷歌来检查连接,所以可以随意使用另一个URL。

func checkInternetConnection(completionHandler:@escaping (Bool) -> Void)
{
    if let url = URL(string: "http://www.google.com/")
    {
        var request = URLRequest(url: url)
        request.httpMethod = "HEAD"
        request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
        request.timeoutInterval = 5

        let tast = URLSession.shared.dataTask(with: request, completionHandler:
        {
            (data, response, error) in

            completionHandler(error == nil)
        })
        tast.resume()
    }
    else
    {
        completionHandler(true)
    }
}

试试这个:

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error


if ([self.delegate respondsToSelector:@selector(getErrorResponse:)]) {
    [self.delegate performSelector:@selector(getErrorResponse:) withObject:@"No Network Connection"];
}

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"BMC" message:@"No Network Connection" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
[alertView show];

}


Swift 3 / Swift 4

您必须首先导入

import SystemConfiguration

您可以通过以下方法检查Internet连接。

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)
}

Alamofire

我知道这个问题是要求Coca Touch解决方案,但我想为搜索检查iOS上的互联网连接的人提供一个解决方案,在这里将有一个更多的选择。

如果您已经在使用Alamofire,以下是您可以从中受益的内容。

你可以将以下类添加到你的应用程序中,并调用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。因为我们开始在单例初始化时监听NetworkReachabilityManager。

另外,为了监视可达性,您需要提供一个主机。目前,我正在使用google.com,但如果需要,请随意更改到任何其他主机或您的主机之一。将类名和文件名更改为与项目匹配的任何名称。


使用Xcode 9和Swift 4.0检查Internet连接的可用性(iOS)

遵循以下步骤

步骤1:

创建一个扩展文件,并将其命名为ReachabilityManager.swift。然后添加下面的代码行。

import Foundation
import SystemConfiguration
public class ConnectionCheck
{
    class func isConnectedToNetwork() -> Bool
    {
        var zeroAddress = sockaddr_in()
        zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
        zeroAddress.sin_family = sa_family_t(AF_INET)

        guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress,
        {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
                SCNetworkReachabilityCreateWithAddress(nil, $0)
            }
        })
        else {
            return false
        }

        var flags: SCNetworkReachabilityFlags = []
        if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
            return false
        }

        let isReachable = flags.contains(.reachable)
        let needsConnection = flags.contains(.connectionRequired)

        return (isReachable && !needsConnection)
    }
}

步骤2:使用下面的代码调用上面的扩展。

if ConnectionCheck.isConnectedToNetwork()
{
     print("Connected")
     // Online related Business logic
}
else{
     print("disConnected")
     // Offline related business logic
}

Pod `Alamofire` has `NetworkReachabilityManager`, you just have to create one function 

func isConnectedToInternet() ->Bool {
        return NetworkReachabilityManager()!.isReachable
}

参见介绍Network.framework: socket的现代替代方案。

我们应该在某些时候摆脱可达性。


对于我的iOS项目,我建议使用

可达性类

Swift声明。对我来说,它工作得很好

Wi-Fi和蜂窝数据

import SystemConfiguration

public class Reachability {

    class func isConnectedToNetwork() -> Bool {

        var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
        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 = SCNetworkReachabilityFlags(rawValue: 0)
        if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
            return false
        }

        let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
        let ret = (isReachable && !needsConnection)
        return ret
    }
}

使用条件语句,

if Reachability.isConnectedToNetwork() {
    // Enter your code here
}
}
else {
    print("NO Internet connection")
}

这个类在几乎所有应用程序使用Internet连接的情况下都很有用。 例如,如果条件为真,就可以调用API或执行任务。


请尝尝这个。它会帮助你的(斯威夫特)

通过CocoaPods或Carthage: Reachability安装 导入可达性并在Network类中使用它 进口可达性 网络{ private let internetReachability:可达性? var isReachable: Bool = false init () { self.internetReachability = Reachability.init() {做 试试self.internetReachability ? .startNotifier () addobserver (self, selector: #selector(self. handlenetworkchange),名称:.reachabilityChanged,对象:internetReachability) } catch { 打印("无法启动可达性通知程序") } } @objc private func handleNetworkChange(通知:通知){ Let可达性=通知。对象!可达性 如果可达性。连接!= .none { 自我。isReachable = true } 其他{ 自我。isReachable = false } print("Internet Connected: \(self.isReachable)") //打印网络连接状态 } } 在你需要的地方使用下面的方法。 var networkOBJ = Network() //使用"networkOBJ. "“网络状态可达” 打印(networkOBJ.isReachable)


//
//  Connectivity.swift
// 
//
//  Created by Kausik Jati on 17/07/20.
// 
//

import Foundation
import Network

enum ConnectionState: String {
    case notConnected = "Internet connection not avalable"
    case connected = "Internet connection avalable"
    case slowConnection = "Internet connection poor"
}
protocol ConnectivityDelegate: class {
    func checkInternetConnection(_ state: ConnectionState, isLowDataMode: Bool)
}
class Connectivity: NSObject {
    private let monitor = NWPathMonitor()
    weak var delegate: ConnectivityDelegate? = nil
    private let queue = DispatchQueue.global(qos: .background)
    private var isLowDataMode = false
    static let instance = Connectivity()
private override init() {
    super.init()
    monitor.start(queue: queue)
    startMonitorNetwork()
}
private func startMonitorNetwork() {
    monitor.pathUpdateHandler = { path in
        if #available(iOS 13.0, *) {
            self.isLowDataMode = path.isConstrained
        } else {
            // Fallback on earlier versions
            self.isLowDataMode = false
        }
        
        if path.status == .requiresConnection {
            print("requiresConnection")
                self.delegate?.checkInternetConnection(.slowConnection, isLowDataMode: self.isLowDataMode)
        } else if path.status == .satisfied {
            print("satisfied")
                 self.delegate?.checkInternetConnection(.connected, isLowDataMode: self.isLowDataMode)
        } else if path.status == .unsatisfied {
            print("unsatisfied")
                self.delegate?.checkInternetConnection(.notConnected, isLowDataMode: self.isLowDataMode)
            }
        }
    
    }
    func stopMonitorNetwork() {
        monitor.cancel()
    }
}

迅捷5号,我是阿拉莫火,主持人

// Session reference
var alamofireSessionManager: Session!

func checkHostReachable(completionHandler: @escaping (_ isReachable:Bool) -> Void) {
    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = 1
    configuration.timeoutIntervalForResource = 1
    configuration.requestCachePolicy = .reloadIgnoringLocalCacheData

    alamofireSessionManager = Session(configuration: configuration)

    alamofireSessionManager.request("https://google.com").response { response in
        completionHandler(response.response?.statusCode == 200)
    }
}

// Using
checkHostReachable() { (isReachable) in
    print("isReachable:\(isReachable)")
}

当使用iOS 12或macOS v10.14 (Mojave)或更新版本时,您可以使用NWPathMonitor而不是史前可达性类。作为奖励,你可以很容易地检测到当前的网络连接类型:

import Network // Put this on top of your class

let monitor = NWPathMonitor()

monitor.pathUpdateHandler = { path in
    if path.status != .satisfied {
        // Not connected
    }
    else if path.usesInterfaceType(.cellular) {
        // Cellular 3/4/5g connection
    }
    else if path.usesInterfaceType(.wifi) {
        // Wi-Fi connection
    }
    else if path.usesInterfaceType(.wiredEthernet) {
        // Ethernet connection
    }
}

monitor.start(queue: DispatchQueue.global(qos: .background))

更多信息请访问:https://developer.apple.com/documentation/network/nwpathmonitor


Swift 5及以后:

public class Reachability {
    class func isConnectedToNetwork() -> Bool {
        var zeroAddress = sockaddr_in()
        zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
        zeroAddress.sin_family = sa_family_t(AF_INET)

        guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
                SCNetworkReachabilityCreateWithAddress(nil, $0)
            }
        }) else {
            return false
        }

        var flags: SCNetworkReachabilityFlags = []
        if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
            return false
        }

        let isReachable = flags.contains(.reachable)
        let needsConnection = flags.contains(.connectionRequired)

        return (isReachable && !needsConnection)
    }

像这样调用这个类:

if Reachability.isConnectedToNetwork() == true {
    // Do something
} else {
    // Do something
}