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

    上边的代码即可满足需求

     

  • 相关阅读:
    iOS NSProgress的使用
    GIT的 .gitignore 配置
    MagicalRecord入门教程
    CoreData的数据存储
    NSLog打印信息的从新设置
    大石头得博客
    exc_bad_access(code=1, address=0x789870)野指针错误
    oc 获取当前设备系统的版本号
    免证书真机调试脚本iphoneentitlements
    支持非arc
  • 原文地址:https://www.cnblogs.com/Apologize/p/5916754.html
Copyright © 2011-2022 走看看