zoukankan      html  css  js  c++  java
  • 有网时自动刷新数据

    这需要检测网络状态,只需要有网和没网两种状态即可(处理多种状态时可以在下边的通知方法netChanged中添加)。这里用到的是第三方框架Reachability

    地址是这里
    在viewController的viewDidLoad中添加监控网络的方法

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor lightGrayColor];
        [self monitorNetworkState];
    }
    /** 监控网络状态 */
    -(void)monitorNetworkState{
        //kReachabilityChangedNotification是Reachability中的通知名称
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netChanged:) name:kReachabilityChangedNotification object:nil];
        Reachability *net = [Reachability reachabilityForInternetConnection];
        [net startNotifier];
    }
    
    -(void)netChanged:(NSNotification *)notification{
        Reachability *reachability = (Reachability *)notification.object;
        NetworkStatus status = reachability.currentReachabilityStatus;
        if (status != NotReachable) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"有网了" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            [alert show];
        }
    }

    上边的代码即可满足需求

     

  • 相关阅读:
    POJ 1887 Testing the CATCHER
    HDU 3374 String Problem
    HDU 2609 How many
    POJ 1509 Glass Beads
    POJ 1458 Common Subsequence
    POJ 1159 Palindrome
    POJ 1056 IMMEDIATE DECODABILITY
    POJ 3080 Blue Jeans
    POJ 1200 Crazy Search
    软件体系结构的艺术阅读笔记1
  • 原文地址:https://www.cnblogs.com/Apologize/p/5916754.html
Copyright © 2011-2022 走看看