iOS程序前后台监听通知

程序退出到后台,控制器的数据还是会存在的,进入到前台时候 不会走viewWillAppear方法了,很多人这里会误导,只有控制器之间来回跳转会进这个;

//哪个控制器需要监听就  写上下面的通知

[[NSNotificationCenter defaultCenter] addObserver:self

                                                selector:@selector(startMonitor)

                                                    name:UIApplicationDidBecomeActiveNotification object:nil];

 

 

      [[NSNotificationCenter defaultCenter] addObserver:self

                                                selector:@selector(closeMonitor)

                                                    name: UIApplicationDidEnterBackgroundNotification object:nil];

    //如果有多个控制器的话 进入到前后和后台的方法会并发一起走

-(void)startMonitor{

    

    if (_timerEveryFive) {

        DLog(@"开始监听%@",selectCard);

        NSString *str=nil;

        if (selectCard.length>0) {

            str=selectCard;

        }

        else{

            if (_cardsArray.count>0) {

                str=_cardsArray[0][@"cardNo"];

            }

        }

       

        [self getSuccessInfo:str];

    }

}

 

-(void)closeMonitor{

    

    if (_timerEveryFive) {

        DLog(@"关闭监听%@",selectCard);

        dispatch_source_cancel(_timerEveryFive);

    }

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值