如何通过可达性检测网络变化?

发布于 2024-12-15 05:02:06 字数 418 浏览 6 评论 0原文

我目前正在使用以下方法检查 viewDidLoad 上的网络连接:

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

但我也希望在网络发生变化时收到通知,例如 wifi 断开或 wifi 恢复,以便我可以进行相应的更改。

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

I'm currently checking network connection on viewDidLoad using this:

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

But I also want to be notified if there is a change of network, such as wifi dropped, or wifi is back, so I can make changes accordingly.

How can I adjust my method to do something like that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜爱皱眉﹌ 2024-12-22 05:02:06

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

2- 从 GitHub 下载以下文件

Reachability.h
Reachability.m

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

4- 在 YourViewController.h 中添加 @class Reachability;

#import <UIKit/UIKit.h>

@class Reachability;

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

#import <UIKit/UIKit.h>

@class Reachability;

@interface YourViewController : UIViewController {
    Reachability* internetReachable;
}

6- 在 YourViewController.m 中添加 Reachability.h

#import "YourViewController.h"
#import "Reachability.h"

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

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

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

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

现在,每次互联网连接的变化您都会在控制台中看到登录。

1- add SystemConfiguration.framework to your project.

2- Download following files from GitHub

Reachability.h
Reachability.m

3- Add these files in your projects

4- add @class Reachability; in YourViewController.h

#import <UIKit/UIKit.h>

@class Reachability;

5- add variable Reachability* internetReachable; in YourViewController.h

#import <UIKit/UIKit.h>

@class Reachability;

@interface YourViewController : UIViewController {
    Reachability* internetReachable;
}

6- add Reachability.h in YourViewController.m

#import "YourViewController.h"
#import "Reachability.h"

7- add following lines in -(void)ViewDidLoad in YourViewController.m

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

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

8- add following function after -(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;
        }
    }
}

Now every change of internet connection you will see log in console.

孤独陪着我 2024-12-22 05:02:06

另一种可能的解决方案是在“application didfinishlaunching”中添加 NS 通知:

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

并在 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
}

Another possible solution is to add a NS Notification in "application didfinishlaunching":

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

and in checkForReachability method do this:

    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
}
云朵有点甜 2024-12-22 05:02:06

我使用了 Donoho Design Group 对 Reachability 类的出色扩展。它具有通知功能,可让您在网络状态发生变化时收到警报。

http://blog.ddg.com/?p=24

祝你好运

I've used the excellent extension to the Reachability class that the Donoho Design Group has put together. It has notifications that allow you to be alerted when the network status changes.

http://blog.ddg.com/?p=24

Good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文