美文网首页
网络监听顶部弹框实现

网络监听顶部弹框实现

作者: 沬日十七 | 来源:发表于2018-03-31 18:01 被阅读0次

    #import "NetworkMonitoring.h"

    #import "Reachability.h"

    @interface NetworkMonitoring()

    /**

     无网络弹框

     */

    @property (nonatomic, strong) UIView * notNetworkView;

    @end

    @implementationNetworkMonitoring

    #pragma mark - 单例

    + (instancetype)shareMonitoring{

        static NetworkMonitoring *instance;

        staticdispatch_once_tonceToken;

        dispatch_once(&onceToken, ^{

            instance = [[selfalloc]init];

        });

        returninstance;

    }

    #pragma mark -注册通知开始监听

    -(void)start

    {

        [[NSNotificationCenter defaultCenter] addObserver:self

                                                 selector:@selector(reachabilityChanged:)

                                                     name:kReachabilityChangedNotificationobject:nil];

        Reachability* reach = [Reachability

                               reachabilityWithHostName:@"www.baidu.com"];

        [reachstartNotifier];

    }

    #pragma mark - 移除监听

    -(void)stop

    {

        [self dissmissView];

    }

    #pragma mark - 网络变化回调

    -(void)reachabilityChanged:(NSNotification*)note{

        Reachability*curReach = [noteobject];

        NetworkStatusstatus = [curReachcurrentReachabilityStatus];

        if(status ==NotReachable){

            [self showAlertView];

        }else

        {

            [self.notNetworkView removeFromSuperview];

        }

    }

    #pragma mark - 懒加载初始化无网络显示UI

    -(UIView*)notNetworkView

    {

        if (_notNetworkView==nil) {

            ///#FF5C5D

            _notNetworkView= [[UIViewalloc]init];

            if(SCREEN_HEIGHT==812) {

                //IphoneX 下移20像素

                _notNetworkView.frame=CGRectMake(0,40,SCREEN_WIDTH,40);

            }else{

                _notNetworkView.frame=CGRectMake(0,20,SCREEN_WIDTH,40);

            }

            _notNetworkView.backgroundColor = [UIColor colorWithRed:255/255 green:92/255 blue:93/255 alpha:1];

            UIImageView* imgView = [[UIImageViewalloc]init];

            imgView.frame=CGRectMake(16,11,18,18);

            [imgViewsetImage: [UIImageimageNamed:@"dl_icon_gth_n"]];

            [_notNetworkViewaddSubview:imgView];

            UIButton  * button = [[UIButtonalloc]init];

            button.frame=CGRectMake(SCREEN_WIDTH-40,0,40,40);

            [buttonsetImage:[UIImage imageNamed:@"dl_icon_close_n"] forState:UIControlStateNormal];

            [buttonaddTarget:self action:@selector(dissmissView) forControlEvents:UIControlEventTouchUpInside];

            [_notNetworkViewaddSubview:button];

            UILabel* label = [[UILabelalloc]init];

            label.frame=CGRectMake(16+18+10,14,SCREEN_WIDTH-(16+18+10-40),13);

            label.font= [UIFontsystemFontOfSize:12];

            label.textColor= [UIColorwhiteColor];

            label.text = @"当前处于断网状态,断网也可以使用摇一摇通哦";

            [_notNetworkViewaddSubview:label];

        }

        return _notNetworkView;

    }

    #pragma mark - 显示

    -(void)showAlertView

    {

        UIWindow * window = [UIApplication sharedApplication].keyWindow;

        [windowaddSubview:self.notNetworkView];

    }

    #pragma mark - 隐藏

    -(void)dissmissView

    {

        [self.notNetworkView removeFromSuperview];

        [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];

    }

    @end

    相关文章

      网友评论

          本文标题:网络监听顶部弹框实现

          本文链接:https://www.haomeiwen.com/subject/sqqncftx.html