iOS 无限后台
2015-06-05 09:40
162 查看
本文实现iOS无限后台的原理是:开机iOS允许的后台定位实现APP在后台无限运行。具体步骤如下
在plist文件里添加:NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,Required background modes 数组添加:App registers for location updates值
然后在AppDelegate.m文件里实现如下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow
alloc]initWithFrame:[[UIScreen
mainScreen]bounds]];
self.window.backgroundColor = [UIColor
whiteColor];
self.window.rootViewController = [[RootViewController
alloc]init];
[self
initData];
[self.window
makeKeyAndVisible];
return
YES;
}
// 进入前台
- (void)applicationWillResignActive:(UIApplication *)application
{
}
// 进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
backgroundTask = [application
beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"applicationD in Background");
}];
}
#pragma mark ---- 私有方法
// 初始化数据
-(void)initData
{
_locationManager = [[CLLocationManager
alloc]
init];
_locationManager.delegate =
self;
_locationManager.distanceFilter =
kCLDistanceFilterNone;
_locationManager.pausesLocationUpdatesAutomatically =
NO;
[_locationManager
startUpdatingLocation];
}
#pragma mark ---- CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//
成功回调
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//
失败回调
}
这样就能实现APP在后台无限运行了。
在plist文件里添加:NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,Required background modes 数组添加:App registers for location updates值
然后在AppDelegate.m文件里实现如下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow
alloc]initWithFrame:[[UIScreen
mainScreen]bounds]];
self.window.backgroundColor = [UIColor
whiteColor];
self.window.rootViewController = [[RootViewController
alloc]init];
[self
initData];
[self.window
makeKeyAndVisible];
return
YES;
}
// 进入前台
- (void)applicationWillResignActive:(UIApplication *)application
{
}
// 进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
backgroundTask = [application
beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"applicationD in Background");
}];
}
#pragma mark ---- 私有方法
// 初始化数据
-(void)initData
{
_locationManager = [[CLLocationManager
alloc]
init];
_locationManager.delegate =
self;
_locationManager.distanceFilter =
kCLDistanceFilterNone;
_locationManager.pausesLocationUpdatesAutomatically =
NO;
[_locationManager
startUpdatingLocation];
}
#pragma mark ---- CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//
成功回调
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//
失败回调
}
这样就能实现APP在后台无限运行了。
相关文章推荐
- 友盟 - iOS 消息推送 (Message) 证书设置指南
- IOS学习路线图
- 小胖说事11-------automaticallyAdjustsScrollViewInsets(个人认为iOS7中略坑爹的属性)
- [iOS]使用symbolicatecrash分析crash文件
- ios客户端学习-AFNetWoriking
- 如何处理iOS中照片的方向
- 轻松学习之 iOS利用Runtime自定义控制器POP手势动画
- ios animate简单动画体验
- Mantle 初步使用
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记29 URL的使用
- 猫猫学iOS 之微博项目实战(1)微博主框架-子控制器的添加
- React.js在ios设备上onClick事件失效的解决办法
- IOS开发---视频录制
- ios中navigationItem的titleView如何居中
- IOS开发中一些类的关系继承图
- ios-日记1
- IOS开发-指纹识别
- IOS-MJExtension一句话搞定对象归档
- IOS GameCenter编程入门
- iOS GCD 开启线程