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
  • 相关阅读:
    WCF 第四章 绑定 在多个绑定上暴露一个服务契约
    WCF 第五章 行为 事务跨操作事务流
    WCF 第五章 导出并发布元数据(服务行为)
    WCF 第五章 行为 通过配置文件暴露一个服务行为
    WCF 第五章 不支持会话的绑定的默认并发和实例
    WCF 第五章 并发和实例(服务行为)
    WCF 第五章 行为 总结
    WCF 第四章 绑定 绑定元素
    WCF 第五章 行为 事务之选择一个事务协议OleTx 或者WSAT
    WCF 第四章 绑定 比较各种绑定的性能和可扩展性
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/4828870.html
Copyright © 2011-2022 走看看