iOS让你的app一直在后台活着(运行)

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

准备工作:

1.导入`AVFoundation.framework`

iOS让你的app一直在后台活着(运行)_第1张图片

2.导入一个无声音乐文件 (.mp3)

3.在info.plist里面请求后台播放音乐的权限


iOS让你的app一直在后台活着(运行)_第2张图片

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];

//创建播放器

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

[audioPlayerprepareToPlay];

//无限循环播放

audioPlayer.numberOfLoops=-1;

[audioPlayerplay];

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

returnYES;

}

-(void)printCurrentTime:(id)sender{

NSLog(@"当前的时间是---%@---",[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



只要不占用太多内存,活着用户手动的kill程序,看打印的log知道这个app可以长时间的在后台运行

你可能感兴趣的:(iOS让你的app一直在后台活着(运行))