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