文章转至http://m.blog.csdn.net/blog/q281494714/40535607
目前IOS能实现后台的 有三种:实时定位,播放音频,以及网络电话;
第一步:
都需要在pilist文件中 申明:
1.IOS8 定位必须实现 NSLocationAlwaysUsageDescription(持续性) /NSLocationWhenInUseUsageDescription(需要时候触发)
2.Required background modes 添加item项 有几种方式1.App registers for location updates (定位) 2.(音乐) 3.(通话)
3.Application does not run background 设置为NO (此项不知道是不是IOS8必须写得)
第二步:
IOS7 之后需要勾选
Capablilities ---->Background Modes--->Location updates (勾选)
Background fetch (勾选)
基本设置已经完成了.
下面我们看代码是如何实现的(定位)
ocationManager= [[CLLocationManager alloc]init];
locationManager.delegate = self;
[locationManager requestAlwaysAuthorization];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
//该模式是抵抗ios在后台杀死程序设置,iOS会根据当前手机使用状况会自动关闭某些应用程序的后台刷新,该语句申明不能够被暂停,但是不一定iOS系统在性能不佳的情况下强制结束应用刷新(重点在这句代码上)
locationManager.pausesLocationUpdatesAutomatically=NO;
[locationManager startUpdatingLocation];
------------------------如果需要每隔多少时间去定位的话 请看已下代码-------------------------
UIBackgroundTaskIdentifier __block bgTask;
// beginBackgroundTaskWithExpirationHandler 向系统借用时间
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (1) {
//开始定位
[self MystartLocationServices];
//定时器
[NSThread sleepForTimeInterval:(ktime1)];
//执行您的方法()
[self culcate];
//停止定位
[self MystopLocationServices];
[NSThread sleepForTimeInterval:(ktime2)];
}
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});