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

    上边的代码即可满足需求

     

  • 相关阅读:
    XML操作类
    输入框样式总结
    根据计算机MAC地址限定每台机子只能领取一次账号
    ico图标的应用
    C#实现关机功能
    在sql中实现数组
    JSON
    MvcHtml.ActionLink()用法
    Brettle.Web.NeatUpload.dll 大文件上传
    asp.net 创建Access数据库
  • 原文地址:https://www.cnblogs.com/Apologize/p/5916754.html
Copyright © 2011-2022 走看看