如何通过Reachability检测网络变化?

Jon*_*Jon 8 iphone cocoa-touch objective-c reachability

我正在检查网络连接viewDidLoad使用它:

-(BOOL)reachable {
    ReachabilityDRC *r = [ReachabilityDRC reachabilityWithHostName:@"google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    if(internetStatus == NotReachable) {
        return NO;
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

但是我也希望在网络发生变化时收到通知,例如wifi掉线或wifi恢复,所以我可以相应地进行更改.

如何调整我的方法来做那样的事情?

Sal*_*lim 9

1-添加SystemConfiguration.framework到您的项目.

2-从GitHub下载以下文件

Reachability.h
Reachability.m
Run Code Online (Sandbox Code Playgroud)

3-在项目中添加这些文件

4-加@class Reachability;YourViewController.h

#import <UIKit/UIKit.h>

@class Reachability;
Run Code Online (Sandbox Code Playgroud)

5-添加变量Reachability* internetReachable;YourViewController.h

#import <UIKit/UIKit.h>

@class Reachability;

@interface YourViewController : UIViewController {
    Reachability* internetReachable;
}
Run Code Online (Sandbox Code Playgroud)

6-加Reachability.hYourViewController.m

#import "YourViewController.h"
#import "Reachability.h"
Run Code Online (Sandbox Code Playgroud)

7-在-(void)ViewDidLoad中添加以下行YourViewController.m

-(void)ViewDidLoad {
    [[NSNotificationCenter defaultCenter] 
                       addObserver:self 
                       selector:@selector(checkNetworkStatus:) 
                       name:kReachabilityChangedNotification 
                       object:nil];

    internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];
}
Run Code Online (Sandbox Code Playgroud)

8-之后添加以下功能 -(void)viewDidLoad

-(void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在每次更改互联网连接都会看到登录控制台.


Ren*_*nes 6

另一种可能的解决方案是在"application didfinishlaunching"中添加NS Notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability) name:kReachabilityChangedNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

并在checkForReachability方法中执行此操作:

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
        //Do something
    }
     else if (remoteHostStatus == ReachableViaWiFi) {
    // Do something
 }
    else{

// Else do something else
}
Run Code Online (Sandbox Code Playgroud)