【iOS系列】-程序开启后台运行
生活随笔
收集整理的這篇文章主要介紹了
【iOS系列】-程序开启后台运行
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
【iOS系列】-程序開啟后臺運行
iOS程序是偽后臺的運行,可是有時候我們需要讓其在后臺也要進(jìn)行一些操作,我們可以讓其偽裝成音樂的APP,這樣就可以讓程序后臺進(jìn)行相關(guān)操作了,具體做法如下:
1:在AppDelegate.m的applicationDidEnterBackground方法中開啟后臺任務(wù)
- (void)applicationDidEnterBackground:(UIApplication *)application {// 開啟后臺任務(wù),讓程序保持運行狀態(tài)[application beginBackgroundTaskWithExpirationHandler:nil]; }2:在info.plist中添加程序的音樂標(biāo)示
新建一個:Required background modes類型的NSArray
并在子類目下新建一個Item 0 其value為App plays audio or streams audio/video using AirPlay
3: 設(shè)置AVAudioSession類型
// 設(shè)置音頻會話類型AVAudioSession *session = [AVAudioSession sharedInstance];//AVAudioSessionCategorySoloAmbient:官方解釋, Use this category for background sounds. Other music will stop playing,也即這個APP播放的時,其他APP會停止播放[session setCategory:AVAudioSessionCategorySoloAmbient error:nil];[session setActive:YES error:nil];轉(zhuǎn)載于:https://www.cnblogs.com/fengtengfei/p/4472399.html
總結(jié)
以上是生活随笔為你收集整理的【iOS系列】-程序开启后台运行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ODAC(V9.5.15) 学习笔记(三
- 下一篇: (笔试题)小米Git