ios 通知监听App进入后台,然后再进入App(获取验证码的时间间隔)
2015-09-22 14:32
435 查看
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];
}
@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];
}
相关文章推荐
- iOS开发-About StoryBoard
- Android项目中如果json字符串中key-value顺序很重要,不要使用Android自带的JSONObject
- Android SDK的DOC文件访问速度过慢的解决办法
- android开发中虚拟键盘的设置参数列表
- android图形系统详解:View layer
- Android内存优化之OOM
- Android中dp和px之间进行转换
- Android进程与线程
- Android各分辨率定义的图片规格
- Qt for Android 部署流程分析
- 【iOS】iOS CocoaPods 整理
- 对于输入框为空及输入的东西只有空格的判断 ios
- Android 解决mac无法识别手机设备
- Android开源工具库
- CTSFail解决办法 android.webkit.cts.WebSettingsTesttestUserAgentString_default 和libcore.java.net.URLfail
- Android的selector,背景选择器
- application/x-www-form-urlencoded
- Unity项目优化--开发项目的小经验
- Android——百度地图开发、添加覆盖物、地理编码和反地理编码、以及覆盖物之间的连线
- Android 5.0 Material Design开发教程