2015-10-16 74 views
0

我使用iBeacon显示模板例如,在我使用本地notifications.when应用不支持后台“didExitRegion”“didEnterRegion”“didRangeBeacons”方法获取调用随机。我不清楚这些方法在后台和后台处理时如何工作,任何人都可以在此协助我。提前感谢。关于应用程序在后台或从后台死亡时ibeacons的行为?

这是示例代码我使用:

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { 
     [manager stopRangingBeaconsInRegion:(CLBeaconRegion*)region]; 
     [self.locationManager stopUpdatingLocation]; 
     NSLog(@"You exited the region."); 
     [self sendLocalNotificationWithMessage:@"You exited the region."]; 
} 

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    [manager startRangingBeaconsInRegion:(CLBeaconRegion*)region]; 
    [self.locationManager startUpdatingLocation]; 
    NSLog(@"You entered the region."); 
    [self sendLocalNotificationWithMessage:@"You entered the region."]; 
} 

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { 
    NSString *message = @"i am in 3 meters."; 
    IMViewController *viewController = (IMViewController*)self.window.rootViewController; 
    viewController.beacons = beacons; 
    [viewController.tableView reloadData]; 

    if(beacons.count > 0) { 
     CLBeacon *nearestBeacon = beacons.firstObject; 
     if(nearestBeacon.proximity == self.lastProximity || 
     nearestBeacon.proximity == CLProximityUnknown) 
     { 
     return; 
     } 
    self.lastProximity = nearestBeacon.proximity; 
    NSLog(@"lastProximity: %ld", (long)self.lastProximity); 
    NSInteger str=(int)nearestBeacon.accuracy; 
    //NSString *distance=[NSString stringWithFormat:@"Distance: %d",(int)nearestBeacon.accuracy]; 
    if (str ==3) 
    { 
     [self sendLocalNotificationWithMessage:message]; 
    }  
    } 
} 
+0

监测:进入/退出区域范围时触发的动作;在前台,背景以及应用程序被杀害时都能工作。 测距:基于接近信标触发的动作;只在前台工作。试试这个教程http://www.raywenderlich.com/66584/ios7-ibeacons-tutorial –

回答

0

我希望这会给你答案。

如果您让重大更改的位置服务继续运行,并且您的iOS应用程序随后被暂停或终止,则服务会在新位置数据到达时自动唤醒您的应用程序。 在起床时间,应用程序被置于后台,您会得到少量时间(大约10秒钟)以手动重新启动位置服务并处理位置数据。 (您必须在任何待处理的位置更新可以交付之前,在后台手动重启位置服务)。

因为你的应用程序在后台,它必须做最少的工作,并避免任何任务(10秒左右),也可以使用beginBackgroundTaskWithName要求更多的后台执行时间:expirationHandler:UiApplication类的方法

注意:当用户全局或为您的应用程序禁用后台应用程序刷新设置时,重大更改位置服务不会启动您的应用程序。此外,当后台应用程序刷新关闭时,即使应用程序处于前台,应用程序也不会收到重大更改或区域监视事件。

因此,当用户进入/退出区域时,同样的情况下也会启动应用程序。

所以请记住两件事

  1. 打开后台应用刷新设置。
  2. 使用beginBackgroundTaskWithName:expirationHandler如果您的代码需要超过5〜10秒。
相关问题