IOS程序设置为音频类app使后台运行

1.Info.plist里设置选项Required background modes 添加item0:App plays audio or streams audio/video using AirPlay
IOS程序设置为音频类app使后台运行_第1张图片

2.设置Capabilities -> Background Modes -> 勾选 Audio,AirPlay**
IOS程序设置为音频类app使后台运行_第2张图片

3.在AppDelegate.m中实现
IOS程序设置为音频类app使后台运行_第3张图片
- (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.
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
});
}

4.以上设置只实现了程序后台运行,要通过审核 还需实现app为音频类app。

你可能感兴趣的:(ios)