iOS讓你的app一直在后臺活著(運行)

原文地址:blog.csdn.net/ios_dashen/article/details/50352204

準備工作:

1.導(dǎo)入`AVFoundation.framework`

2.導(dǎo)入一個無聲音樂文件 (.mp3)

3.在info.plist里面請求后臺播放音樂的權(quán)限


4.上代碼


#import "AppDelegate.h"

#import?

@interfaceAppDelegate?()

@property(strong,nonatomic)NSString*startTime;

@end

@implementationAppDelegate

-?(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions?{

//?Override?point?for?customization?after?application?launch.

AVAudioSession*session?=?[AVAudioSessionsharedInstance];

[sessionsetActive:YESerror:nil];

[sessionsetCategory:AVAudioSessionCategoryPlaybackerror:nil];

//讓?app?支持接受遠程控制事件

[[UIApplicationsharedApplication]beginReceivingRemoteControlEvents];

//播放背景音樂

NSString*musicPath=[[NSBundlemainBundle]pathForResource:@"wusheng"ofType:@"mp3"];

NSURL*url=[[NSURLalloc]initFileURLWithPath:musicPath];

//創(chuàng)建播放器

AVAudioPlayer*audioPlayer=[[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:nil];

[audioPlayerprepareToPlay];

//無限循環(huán)播放

audioPlayer.numberOfLoops=-1;

[audioPlayerplay];

[NSTimerscheduledTimerWithTimeInterval:1.ftarget:selfselector:@selector(printCurrentTime:)userInfo:nilrepeats:YES];

returnYES;

}

-(void)printCurrentTime:(id)sender{

NSLog(@"當(dāng)前的時間是---%@---",[selfgetCurrentTime]);

}

-(NSString*)getCurrentTime{

NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];

[dateFormattersetDateFormat:@"yyyy-MM-DD?HH:mm:ss"];

NSString*dateTime=[dateFormatterstringFromDate:[NSDatedate]];

self.startTime=dateTime;

returnself.startTime;

}

-?(void)applicationWillResignActive:(UIApplication*)application?{

//?Sent?when?the?application?is?about?to?move?from?active?to?inactive?state.?This?can?occur?for?certain?types?of?temporary?interruptions?(such?as?an?incoming?phone?call?or?SMS?message)?or?when?the?user?quits?the?application?and?it?begins?the?transition?to?the?background?state.

//?Use?this?method?to?pause?ongoing?tasks,?disable?timers,?and?throttle?down?OpenGL?ES?frame?rates.?Games?should?use?this?method?to?pause?the?game.

}

-?(void)applicationDidEnterBackground:(UIApplication*)application?{

//?Use?this?method?to?release?shared?resources,?save?user?data,?invalidate?timers,?and?store?enough?application?state?information?to?restore?your?application?to?its?current?state?in?case?it?is?terminated?later.

//?If?your?application?supports?background?execution,?this?method?is?called?instead?of?applicationWillTerminate:?when?the?user?quits.

}

-?(void)applicationWillEnterForeground:(UIApplication*)application?{

//?Called?as?part?of?the?transition?from?the?background?to?the?inactive?state;?here?you?can?undo?many?of?the?changes?made?on?entering?the?background.

}

-?(void)applicationDidBecomeActive:(UIApplication*)application?{

//?Restart?any?tasks?that?were?paused?(or?not?yet?started)?while?the?application?was?inactive.?If?the?application?was?previously?in?the?background,?optionally?refresh?the?user?interface.

}

-?(void)applicationWillTerminate:(UIApplication*)application?{

//?Called?when?the?application?is?about?to?terminate.?Save?data?if?appropriate.?See?also?applicationDidEnterBackground:.

}

@end



只要不占用太多內(nèi)存,活著用戶手動的kill程序,看打印的log知道這個app可以長時間的在后臺運行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容