美文网首页
iOS让你的app一直在后台活着(运行)

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

作者: _假装在上海 | 来源:发表于2017-04-21 15:14 被阅读0次

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

准备工作:

1.导入`AVFoundation.framework`

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

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

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 后台 Crash 调查

    Apple 一直在逐步放大 App 后台运行的权限,到今天为止,已知的 iOS App 后台运行场景有: Back...

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

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

  • iOS让App后台运行

    一般来说,如果不进行后台申请,在iOS系统上,当应用退到后台后,只有5s的时间去执行代码,之后将进入挂起状态。只有...

  • iOS 后台刷新

    [TOC] iOS 后台刷新 首先大概介绍下iOS的APP运行状态简介和后台运行的一些基础知识 1. App运行状...

  • iOS 点击推送跳转详情页

    系统 API :iOS < 7、 7 <= iOS < 10、 iOS >= 10app 状态:后台运行、 前台...

  • iOS 后台持续定位

    前言 前文讲到程序推到后台的运行情况iOS 对APP推到后台运行时长的探究,主要还是想做个后台定位,希望APP在按...

  • iOS让App后台运行方法小结

    一般App进入后台之后,超过了后台运行时间,便进入了挂起状态,无法执行代码,但是内存并没有清除。主要用到2个方法:...

  • iOS开发中的小技巧和思路 (二)

    1.如何让你的app在后台持续运行一段时间 iOS的app在按下home键的时候,只有不到五秒钟的时间去处理保存或...

  • Paper Collection - Background Ta

    1.IOS后台运行机制详解(一)2.IOS后台运行机制详解(二)3.IOS后台运行 之 后台播放音乐4.转载:IO...

  • iOS后台模式借助位置更新实现

    需求:iOS系统下使我们的app在后台下(点击Home键进入后台)仍能继续运行任务. 阅读前提: 了解后台任务机制...

网友评论

      本文标题: iOS让你的app一直在后台活着(运行)

      本文链接:https://www.haomeiwen.com/subject/hxmdzttx.html