iOS App运行在后台实现定位以及播放音频功能
来源:互联网 发布:模拟量数据采集模块 编辑:程序博客网 时间:2024/06/08 04:34
最近再做一个类似于“驾考宝典”的iOS应用,当然作为考试计时的功能肯定不会少,开发期间也是遇到各种各样的小问题,本文所讲为其中之一:iOS程序进入后台后十分钟之内就会被系统kill掉,怎么解决呢?我想要程序进入后台后仍然运行计时功能,否则就无法达到考试的目的,之后在网上查阅了相关资料最后终于找到答案,其精髓就是:利用苹果给出的三种类型的程序可以保持在后台运行:音频播放类,位置更新类,另外一个记不太清楚了,我利用了苹果给出的音频播放类的这个“特权”来满足我程序上的要求,详细步骤如下:
1、步骤一:在Info.plist中,添加"Required background modes"键,value为:App plays audio
步骤二:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: &setCategoryErr];
[[AVAudioSession sharedInstance]
setActive: YES
error: &activationErr];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
步骤三:将以下代码添加到appDelegate文件中的- (void)applicationDidEnterBackground:(UIApplication *)application函数,也可添加到在具体类中注册的应用进入后台后的通知方法
- (void)applicationDidEnterBackground:(UIApplication *)application{
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;
}
});
});
}
完成以上步骤你会发现,程序进入后台后仍可运行定时器!
实现后台定位功能
最近终于回归iOS了,一会来就碰到了定位相关的功能要求,客户要求即使应用挂起也可以执行GPS定位,并在位置发生变化时根据你的需要唤醒应用。
稍微研究了一下,实现方式跟音频后台播放类似,在应用的plist里添加后台运行设定即可。
Apple官方说明原文:
An app that provides continuous location updates to the user (even when in the background) can enable background location services by including the UIBackgroundModes key (with the location value) in its Info.plist file. The inclusion of this value in the UIBackgroundModes key does not preclude the system from suspending the app, but it does tell the system that it should wake up the app whenever there is new location data to deliver. Thus, this key effectively lets the app run in the background to process location updates whenever they occur.
实现方法是,在应用的info.plist里找到【Required background modes】项,在其中添加如下item: 【App registers for location updates】。
当上述设定完成后,只要你在应用中打开了GPS定位功能,即使程序挂起,仍然能够获取GPS定位信息。
有两点问题需要注意:
1. 后台运行GPS定位非常消耗电量,所以建议在后台使用时降低GPS定位精度以节省用电,并确定你的用户确实需要这个功能,并在应用中给出提示说明。
2. 后台运行GPS定位有时间限制,大约10分钟时间,超过时间后系统会终止后台定位。
- iOS App运行在后台实现定位以及播放音频功能
- iOS在后台运行app
- iOS 实现在后台播放音乐
- iOS实现在后台播放音乐
- iOS实现在后台播放音乐
- iOS实现在后台播放音乐
- iOS-----实现在后台播放音乐
- 设置App运行在后台
- iOS让你的app一直在后台活着(运行)
- APP更新功能,使用Notification实现在后台更新
- ios 程序在后台运行
- ios 实现在后台运行的长期任务
- 如何禁止App在后台运行以及如何保存和恢复App的状态
- 如何禁止App在后台运行以及如何保存和恢复App的状态
- 如何禁止App在后台运行以及如何保存和恢复App的状态
- Android 判断app是否运行在后台
- 判断App是否在后台运行
- 如何让安卓APP一直在后台运行?
- 金融IC卡文件结构及访问机制
- spring定时器时间配置(转)
- ECshop商品详情页显示可赠送积分和购买使用金额积分
- meta viewport 标签亲测 (待续)
- android创建提示音
- iOS App运行在后台实现定位以及播放音频功能
- Rehhat 6.4 Linux最小化系统中安装桌面与中文支持
- 张广慧:云计算对游戏开发者的价值
- Raysoft.Framework.Encrypt.EncryptHelper
- 单缓冲区--生产者-消费者问题
- OpenGL(5)GLUT库建模及视频播放API
- 黑马程序员_交通灯管理系统
- EXTJS入门教程及其框架搭建
- CSS命名规范(规则)