iOS有兩種后臺運行北疗猓活方式,第一種叫無聲音樂鼻笠。活(即在后臺開啟音頻播放射沟,只不過不需要播放出音量且不能影響其他音樂播發(fā)軟件),第二種叫Background Task与境,但是這種方法在iOS 13以后只能申請短短的30秒鐘時間验夯,但是在iOS7-iOS13以前是可以申請到3分鐘的保活時間的摔刁,當(dāng)然我們也可以經(jīng)過處理來申請到更多的被幼活時間。
無聲音樂惫睬活
1.打開應(yīng)用的Target頁面Signing & Cabailities绑谣,添加Capability(Background Modes)勾選Audio,AirPlay拗引,and Picture in Picture選項
2.在info.plist文件的【Required background modes】中借宵,添加對應(yīng)的key:【App plays audio or streams audio/video using AirPlay】
3.在AppDelegate.m中添加監(jiān)聽
UIApplicationWillEnterForegroundNotification(應(yīng)用進入前臺通知)UIApplicationDidEnterBackgroundNotification(應(yīng)用進入后臺通知)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
4.編寫音樂播放類
.h文件
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface BackgroundPlayer : NSObject <AVAudioPlayerDelegate>
{
AVAudioPlayer* _player;
}
- (void)startPlayer;
- (void)stopPlayer;
@end
.m文件
#import "BackgroundPlayer.h"
@implementation BackgroundPlayer
- (void)startPlayer
{
if (_player && [_player isPlaying]) {
return;
}
AVAudioSession *session = [AVAudioSession sharedInstance];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
NSString* route = [[[[[AVAudioSession sharedInstance] currentRoute] outputs] objectAtIndex:0] portType];
if ([route isEqualToString:AVAudioSessionPortHeadphones] || [route isEqualToString:AVAudioSessionPortBluetoothA2DP] || [route isEqualToString:AVAudioSessionPortBluetoothLE] || [route isEqualToString:AVAudioSessionPortBluetoothHFP]) {
if (@available(iOS 10.0, *)) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:(AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP)
error:nil];
} else {
// Fallback on earlier versions
}
}else{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:(AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker)
error:nil];
}
[session setActive:YES error:nil];
NSString *path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[_player prepareToPlay];
[_player setDelegate:self];
_player.numberOfLoops = -1;
BOOL ret = [_player play];
if (!ret) {
NSLog(@"play failed,please turn on audio background mode");
}
}
- (void)stopPlayer
{
if (_player) {
[_player stop];
_player = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:NO error:nil];
NSLog(@"stop in play background success");
}
}
@end
將使用到的音樂文件放入項目:
5.在應(yīng)用進入后臺時開啟保活
在AppDelegate.m文件中矾削,編寫如下代碼:
// 語音播報
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
@property (nonatomic, strong) BackgroundPlayer* player;
- (void)appWillEnterForeground {
if (self.player) {
[self.player stopPlayBackgroundAlive];
}
}
- (void)appDidEnterBackground {
if (_player == nil) {
_player = [[BackgroundPlayer alloc] init];
}
[self.player startPlayer];
}
Background Task比烂担活
1.在info.plist文件的【Required background modes】中,添加對應(yīng)的key:【App registers for location updates】
2.同樣我們需要監(jiān)聽
UIApplicationWillEnterForegroundNotification(應(yīng)用進入前臺通知)和UIApplicationDidEnterBackgroundNotification(應(yīng)用進入后臺通知)
在AppDelegate.m文件中編寫如下代碼:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
- (void)appWillEnterForeground {}
- (void)appDidEnterBackground {}
3.使用Background Task申請焙呖活時間欲间,在應(yīng)用進入后臺時開啟保活断部,在應(yīng)用進入前臺時關(guān)閉绷蕴活:
在AppDelegate.m文件中編寫如下代碼:
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundId;
- (void)appWillEnterForeground {
[self stopKeepAlive];
}
- (void)appDidEnterBackground {
_backgroundId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
//申請的時間即將到時回調(diào)該方法
NSLog(@"BackgroundTask time gone");
[self stopKeepAlive];
}];
}
- (void)stopKeepAlive{
if (_backgroundId) {
[[UIApplication sharedApplication] endBackgroundTask:_backgroundId];
_backgroundId = UIBackgroundTaskInvalid;
}
}
4.使用NSTimer循環(huán)申請保活時間,但是建議不要無限申請彼剩活時間达址,因為系統(tǒng)如果發(fā)現(xiàn)該應(yīng)用一直在后臺運行時,是可能會直接crash掉你的應(yīng)用的 惹骂,錯誤碼0x8badf00d.
將需要在后臺一直運行的代碼苏携,寫在AppDelegate.m中運行:
例如:像后臺發(fā)送手機定位:
// 高德地圖
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
//定位
@property (nonatomic,strong) AMapLocationManager *locationManager;
//注冊定位信息
@property (nonatomic,strong) NSMutableDictionary * locationDic;
// 定位
self.locationDic = [@{} mutableCopy];
[self locationPresent];
//開啟定時器(后臺每五分鐘上傳一次定位)
[self StartTimer];
// 開啟倒計時效果
- (void)StartTimer{
__block NSInteger time = 10; //倒計時時間
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0);//每秒執(zhí)行
dispatch_source_set_event_handler(timer, ^{
if(time <= 0){ //倒計時結(jié)束,關(guān)閉
dispatch_source_cancel(timer);
dispatch_async(dispatch_get_main_queue(), ^{
[self StartTimer];
//上傳定位信息
[self locationPresent];
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
});
time--;
}
});
dispatch_resume(timer);
}
//定位
-(void)locationPresent{
self.locationManager = [[AMapLocationManager alloc] init];
[self.locationManager setDelegate:self];
@try {
self.locationManager.allowsBackgroundLocationUpdates = YES;
} @catch (NSException *exception) {
NSLog(@"異常:%@", exception);
} @finally {
}
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// 帶逆地理信息的一次定位(返回坐標(biāo)和地址信息)
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
// 設(shè)置不允許系統(tǒng)暫停定位
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
// 定位超時時間对粪,最低5s右冻,此處設(shè)置為5s
self.locationManager.locationTimeout = 5;
// 逆地理請求超時時間,最低5s著拭,此處設(shè)置為5s
self.locationManager.reGeocodeTimeout = 5;
[self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error){
if (error){
LLLog(@"%@",error);
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
if (error.code == AMapLocationErrorLocateFailed){
return;
}
}
NSLog(@"location:%@", location);
if (location) {
[self.locationDic setValue:@(location.coordinate.latitude) forKey:@"latitude"];
[self.locationDic setValue:@(location.coordinate.longitude) forKey:@"longitude"];
// 上傳定位信息
[self UpdateLocationPlace];
}
}];
}
//上傳當(dāng)前位置
-(void)UpdateLocationPlace{
NSString *Url = [NSString stringWithFormat:@"%@%@",BaseUrl,UploadForDriver];
LLLog(@"UpdateLocationPlace===%@===%@",Url,self.locationDic);
[SendHttpRequest PostNSMutableDictionary:self.locationDic withNsstring:Url result:^(NSDictionary *result, NSError *error) {
LLLog(@"%@",result);
}];
}
轉(zhuǎn)載自:https纱扭。//blog.csdn.net/qq_38520096/article/details/102626210