zoukankan      html  css  js  c++  java
  • ios 通知监听App进入后台,然后再进入App(获取验证码的时间间隔)

    1.自定义按钮继承与UIButton

    @interface SMSButton ()

    {

        int _timerNumber; //定时器时间

        long long int _backGroundInterval;//时间戳

    }

    @property (nonatomic, strong) NSTimer *smsTime;

    @property (nonatomic, strong) UIActivityIndicatorView *indicatorView;

     

    @end

    @implementation SMSButton

    2.初始化方法

    -(id)initWithCoder:(NSCoder *)aDecoder

    {

        self = [super initWithCoder:aDecoder];

        if (self) {

            self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

            [self addSubview:self.indicatorView];

            self.indicatorView.center = CGPointMake(self.width/2, self.height/2);

            self.indicatorView.hidden = YES;

            

            [self addObserver];

        }

        return self;

    }

    3.掉接口获取验证码

    - (void)startWithPhoneNumber:(NSString *)phoneNumber

    {

        [self startIndicator];

        NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];

        [parametersDic setObject_package:phoneNumber forKey:@"telephone" performMethodInfo:MDIC_PERFORMMETHODINFO];

        [PHHTTPManager postPath:MobileSend paraDict:parametersDic success:^(NSString *respMsg, NSString *respCode, NSDictionary *responseObject) {

            [self stopIndicator];

            [self startTimer];

            ALERTSHOW(@"验证码已发送");

            

        } failure:^(NSString *errorMsg, NSUInteger errorCode) {

            [self stopIndicator];

            [self resetState];

        }];

      4.设置网络请求的加载提示和设置定时器

    -(void)startIndicator

    {

        [self setupTitle:@""];

        self.indicatorView.hidden = NO;

        [self.indicatorView startAnimating];

        self.userInteractionEnabled = NO;

    }

     

    -(void)stopIndicator

    {

        [self.indicatorView stopAnimating];

        self.indicatorView.hidden = YES;

    }

     

    -(void)startTimer

    {

        _timerNumber = 120;

        self.smsTime = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(handleNumberTimer:) userInfo:nil repeats:YES];

    }

     

    - (void)handleNumberTimer:(id)sender

    {

        _timerNumber--;

        if (_timerNumber > 0) {

            [self handleNumberTimerRunning];

        }else{

            [self handleNumberTimerStop];

        }

    }

     

    - (void)handleNumberTimerRunning

    {

        [self setupTitle:[NSString stringWithFormat:@"%d",_timerNumber]];

    }

     

    - (void)handleNumberTimerStop

    {

        [self resetState];

    }

     

    - (void)resetState

    {

        [_smsTime invalidate];

        _smsTime = nil;

        self.userInteractionEnabled = YES;

        [self setupTitle:@"获取验证码"];

    }

     

    - (void)setupTitle:(NSString *)titleStr

    {

        [self setTitle:titleStr forState:UIControlStateNormal];

    }

     5.注册监听者

    //监听通知

    -(void)addObserver

    {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppDidBackGround) name:UIApplicationDidEnterBackgroundNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppDidEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];

    }

     

    //程序进入后台

    -(void)handleAppDidBackGround

    {

        _backGroundInterval = (long long int)[[NSDate date] timeIntervalSince1970];//取当前时间戳

    }

     

    //程序进入前台

    -(void)handleAppDidEnterForeground

    {

        long long int foreInterval = (long long int)[[NSDate date] timeIntervalSince1970];//取当前时间戳

        

        int differ = (int)( foreInterval  - _backGroundInterval);

        

        _timerNumber = (_timerNumber >= 0)?(_timerNumber - differ):_timerNumber;

    }

     

    6.移除监听者

     

    //移除通知

    -(void)removeObserver

    {

        [[NSNotificationCenter defaultCenter] removeObserver:self];

    }

     

    -(void)dealloc

    {

        [self removeObserver];

    }

     

    1
  • 相关阅读:
    git gui 学习
    Java中关于 ArrayList 和 Map 的常用遍历方法 (学习笔记,便于以后查询)
    关于对 NUMA 理解(学习笔记,便于以后查阅)
    Java判断数据库表是否存在的方法
    流程开发Activiti 与SpringMVC整合实例
    shiro权限管理框架与springmvc整合
    Java开发中的23种设计模式详解(转)
    JMS学习之路(一):整合activeMQ到SpringMVC
    SpringMVC中定时任务配置
    RPC远程过程调用学习之路(一):用最原始代码还原PRC框架
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/4828870.html
Copyright © 2011-2022 走看看