Ios 在后台模式下循环
我想在后台搜索RSSI。我试图通过调用同一个函数中的函数来实现这一点。有没有办法让这个循环在后台模式下运行。在我看来,它应该是生活在背景中,但我在经历了无数次灾难后就死了。如何让它不断搜索Ios 在后台模式下循环,ios,objective-c,appdelegate,Ios,Objective C,Appdelegate,我想在后台搜索RSSI。我试图通过调用同一个函数中的函数来实现这一点。有没有办法让这个循环在后台模式下运行。在我看来,它应该是生活在背景中,但我在经历了无数次灾难后就死了。如何让它不断搜索 - (void)applicationWillResignActive:(UIApplication *)application { if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
- (void)applicationWillResignActive:(UIApplication *)application
{
if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
NSLog(@"Multitasking Supported");
}
else
{
NSLog(@"Multitasking Not Supported");
}
NSLog(@"Start background task");
mBeaconDeviceList = [[NSMutableArray alloc] init];
mBeaconConfigManager = [[JLEBeaconConfigManager alloc] init];
mBeaconConfigManager.delegate = self;
[mBeaconConfigManager startJaaleeBeaconsDiscovery];
}
#pragma mark - JLEBeaconConfigManager delegate
-(void)beaconConfigManager:(JLEBeaconConfigManager *)manager didDiscoverBeacon:(JLEBeaconDevice *)beacon RSSI:(NSNumber *)RSSI AdvData:(NSDictionary *)AdvData
{
NSLog(@"Find RSSI");
if ([RSSI intValue] > 0 || [RSSI intValue] < -40) {
return;
}
if ([mBeaconDeviceList containsObject:beacon]) {
[mBeaconDeviceList removeObject:beacon];
}
[mBeaconDeviceList addObject:beacon];
NSLog(@"FOUND: %@", RSSI);
NSLog(@"Start again");
mBeaconDeviceList = [[NSMutableArray alloc] init];
mBeaconConfigManager.delegate = self;
[mBeaconConfigManager startJaaleeBeaconsDiscovery];
}
-(void)应用程序将重新指定:(UIApplication*)应用程序
{
如果([[UIDevice currentDevice]响应选择器:@selector(isMultitaskingSupported)])
{
NSLog(@“支持多任务”);
}
其他的
{
NSLog(@“不支持多任务”);
}
NSLog(@“启动后台任务”);
mBeaconDeviceList=[[NSMutableArray alloc]init];
mBeaconConfigManager=[[JLEBeaconConfigManager alloc]init];
mBeaconConfigManager.delegate=self;
[MBeAconfigManager启动JaaleebaconsDiscovery];
}
#pragma标记-jlebaconconfigmanager委托
-(void)BeanConfigmanager:(JLEBeaconConfigManager*)manager didDiscoverBeacon:(JLEBeaconDevice*)信标RSSI:(NSNumber*)RSSI AdvData:(NSDictionary*)AdvData
{
NSLog(“查找RSSI”);
如果([RSSI intValue]>0 | |[RSSI intValue]<-40){
返回;
}
if([mBeaconDeviceList containsObject:beacon]){
[mBeaconDeviceList removeObject:beacon];
}
[mBeaconDeviceList addObject:beacon];
NSLog(@“发现:%@”,RSSI);
NSLog(@“重新启动”);
mBeaconDeviceList=[[NSMutableArray alloc]init];
mBeaconConfigManager.delegate=self;
[MBeAconfigManager启动JaaleebaconsDiscovery];
}
您可以将后台时间延长到3分钟,这将使您有更多的时间从RSSIs进行搜索。您可以在ApplicationIdentinterBackground中进行如下扩展:
- (void)extendBackgroundRunningTime {
if (_backgroundTask != UIBackgroundTaskInvalid) {
// if we are in here, that means the background task is already running.
// don't restart it.
return;
}
NSLog(@"Attempting to extend background running time");
__block Boolean self_terminate = YES;
_backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"DummyTask" expirationHandler:^{
NSLog(@"Background task expired by iOS");
if (self_terminate) {
[[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
_backgroundTask = UIBackgroundTaskInvalid;
}
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Background task started");
while (true) {
NSLog(@"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining);
[NSThread sleepForTimeInterval:1];
}
});
}
我认为现在时间不是问题。问题是方法
beaconfigmanager
在applicationwillresignative
调用,但当它转到applicationidentinterbackground
时,方法beaconfigmanager
停止发送响应。