iOS开发部落代码片段iOS开发转发

在后台执行短信验证码倒计时

2017-03-01  本文已影响1506人  甘邦
前言

我们一般做的短信验证码功能,应用在前台运行的时候,肯定是没有问题的,但是点击一下Home键进入后台挂起的状态,我们的定时器就停止了。

网上很多说记录什么进入后台的时间,我觉的那太费事了,我们完全可以通过申请应用在后台执行来执行我们的倒计时。

1、先放我们倒计时的代码,这段代码在模拟器运行是没有问题的,但是在真机进入挂起状态会停止

<pre>

2、下面是我们用到的一个小技巧

一般来说,如果不进行后台申请,在iOS系统上,当应用退到后台后,只有5s的时间去执行代码,之后将进入挂起状态。只有像音频播放、定位、newsstand、VoIP等功能才能持续在后台运行。但是开发其它应用是我们可以通过申请后台,来获得3分钟的后台执行代码时间(iOS7以前是10分钟)。

所以我们决定申请3分钟来执行我们1分钟的倒计时代码:
<pre>

import "AppDelegate.h"

@interface AppDelegate (){
NSInteger count;
}
@property(strong, nonatomic)NSTimer *mTimer;
@property(assign, nonatomic)UIBackgroundTaskIdentifier backIden;

@end

@implementation AppDelegate

//计时
-(void)countAction{
NSLog(@"%li",count++);
}

//申请后台
-(void)beginTask
{
NSLog(@"begin=============");
_backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
//在时间到之前会进入这个block,一般是iOS7及以上是3分钟。按照规范,在这里要手动结束后台,你不写也是会结束的(据说会crash)
NSLog(@"将要挂起=============");
[self endBack];
}];
}

//注销后台
-(void)endBack
{
NSLog(@"end=============");
[[UIApplication sharedApplication] endBackgroundTask:_backIden];
_backIden = UIBackgroundTaskInvalid;
}

@end
</pre>

这样就轻松、完美解决了我们的需求了!!!

3、 扩充无限倒计时

慎用!因为这个需要申请后台播放音频的权限。如果你的应用不是相关应用,AppStore审核可能不会通过。

先在我们的info.plist文件配置中,添加一行,如下两个属性:

Required background modes
App plays audio or streams audio/video using AirPlay

配置信息.png

AppDelegate.m文件中代码还是和上面一样,设置 UIBackgroundTaskIdentifier
<pre>

import "AppDelegate.h"

@interface AppDelegate (){
NSInteger count;
}
@property(strong, nonatomic)NSTimer *mTimer;
@property(assign, nonatomic)UIBackgroundTaskIdentifier backIden;

@end

@implementation AppDelegate

//计时
-(void)countAction{
NSLog(@"%li",count++);
}

//申请后台
-(void)beginTask
{
NSLog(@"begin=============");
_backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

    NSLog(@"将要挂起=============");
    [self endBack];
}];

}

//注销后台
-(void)endBack
{
NSLog(@"end=============");
[[UIApplication sharedApplication] endBackgroundTask:_backIden];
_backIden = UIBackgroundTaskInvalid;
}

@end
</pre>

在我们需要后台运行的控制器中,例如ViewController.m
<pre>

import "ViewController.h"

import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property(strong, nonatomic)AVAudioPlayer *mPlayer;

@property(assign, nonatomic)CGFloat mCount;

@end

@implementation ViewController

-(void)countTime{
_mCount+=10;
NSLog(@"%f",_mCount);

if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 60.) {//当剩余时间小于60时,开如播放音乐,并用这个假前台状态再次申请后台
    NSLog(@"播放%@",[NSThread currentThread]);
    [self playMusic];
    //申请后台
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"我要挂起了");
    }];
}

}

-(void)playMusic{
//1.音频文件的url路径,实际开发中,用无声音乐
NSURL *url=[[NSBundle mainBundle]URLForResource:@"欢沁.mp3" withExtension:Nil];

//2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
_mPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];

//3.缓冲
[_mPlayer prepareToPlay];

//4.播放
[_mPlayer play];

}

@end
</pre>

完。

自助者,天助之。

上一篇下一篇

猜你喜欢

热点阅读