Gavin.han

致力于移动开发 技术改变生活
随笔 - 133, 文章 - 0, 评论 - 46, 阅读 - 42万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

ios 网络监测之Reachability

Posted on   gavin.han  阅读(420)  评论(0编辑  收藏  举报

使用之前请从Apple网站下载示例:点此下载

然后将Reachability.h 和 Reachability.m 加到自己的项目中,并引用 SystemConfiguration.framework,就可以使用了。

效果1:

 

 

复制代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    //检测网络情况
    [self startNotificationNetwork];
  
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)reachabilityChanged:(NSNotification *)notification{
    Reachability *currentReach = [notification object];
    NSParameterAssert([currentReach isKindOfClass:[Reachability class]]);
    NetworkStatus status = [currentReach currentReachabilityStatus];
    NSString *netMsg = nil;
    switch (status) {
        case NotReachable:
        {
            netMsg = @"网络不可用";
            break;
        }
        case ReachableViaWiFi:
        {
            netMsg = @"通过WiFi上网";
            break;
        }
        case ReachableViaWWAN:
        {
            netMsg = @"通过3G/GPRS上网";
            break;
        }
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"联网提示" message:netMsg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}


-(void)startNotificationNetwork{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    Reachability * hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];
    [hostReach startNotifier];
}
复制代码

 

致谢:http://www.cnblogs.com/mrhgw/archive/2012/08/01/2617760.html

 效果2:

复制代码
//处理连接改变后的情况 //对连接改变做出响应的处理动作。
- (void)updateInterfaceWithReachability: (Reachability*) curReach
{
    NetworkStatus status = [curReach currentReachabilityStatus];
    
    if(status ==NotReachable) {
        UIAlertView*alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示"
                                                          message:@"网络连接失败,请检查网络"
                                                         delegate:nil
                                                cancelButtonTitle:@"确定"
                                                otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }else{
        NSLog(@"connect with the internet successfully");
    }
    
}


 //连接改变
- (void)reachabilityChanged:(NSNotification* )note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self updateInterfaceWithReachability: curReach];
}


-(void)startNotificationNetwork{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    Reachability * hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];
    [hostReach startNotifier];:
}
复制代码

致谢:http://blog.sina.com.cn/s/blog_91ff71c001016gql.html

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示