位置管理器在"始终"上工作,但不在使用中时工作



我有一个应用程序,在苹果坚持让用户在位置管理器的"始终"和"使用时"之间进行选择之前,它运行良好。 该应用程序使用iBeacons发送玩游戏和接受的邀请.
当选择"总是"时,信标工作正常,但当我切换到"使用时"它们根本不起作用. 我开始使用"总是",但更改以下代码以为用户提供选择。 在应用程序的列表中,我添加了"隐私位置始终和使用时使用说明和使用中的隐私位置",并删除了"隐私位置始终使用说明"。

在应用程序的代表中,我有这个

- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedAlways){
NSLog(@"Always");
AlertView2 = [[UIAlertView alloc] initWithTitle:@"Dual player on two devices is enabled."
message:@"To save battery power go to Settings/Privacy/Location Services and choose "Never" when not using I'M GAME. Two people can still play on one device when in "Never" mode. To recieve invitations to play only when the app is open select "When In Use"."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[AlertView2 show];
[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"accessKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
NSLog(@"WhenInUse");
AlertView2 = [[UIAlertView alloc] initWithTitle:@"Dual player on two devices is enabled."
message:@"To save battery power go to Settings/Privacy/Location Services and choose "Never" when not using I'M GAME. Two people can still play on one device when in "Never" mode. To recieve invitations to play while app is in background select "Always"."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[AlertView2 show];
[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"accessKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusRestricted){
NSLog(@"restricted");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
NSLog(@"denied");
AlertView2 = [[UIAlertView alloc] initWithTitle:@"Dual player on a single device Only."
message:@"To play on two devices go to Settings Privacy/Location Services and choose "Always" or "When In Use" for I'M GAME. In "Always" you can recieve invites while app is in background, in "When In Use" invites only appear when the app is on screen. To preserve battery choose "Never" when not using I'M GAME."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[AlertView2 show];
[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"accessKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
NSLog(@"undetermined2");
[locationManager requestAlwaysAuthorization];
[locationManager requestWhenInUseAuthorization];
}

}

iBeacon是否需要将隐私位置设置为"始终"才能工作?

所以我刚刚发现在"使用时"您无法监控信标区域是否进入或退出只能找到其范围. 所以我想问题是我将如何使用范围向我的用户发送通知。

当你授权你的应用仅在前台时执行信标范围时, 只需一个didRangeBeacons回调即可轻松模拟进入/退出逻辑.

  1. 设置一个类变量:

    var beaconLastSeen: Date? = nil
    
  2. 将此代码添加到didRangeBeacons方法:

    if beacons.count > 0 {
    if beaconLastSeen == nil {
    // call didEnterRegion logic here
    }
    beaconLastSeen = Date()
    }
    else {
    if beconLastSeen != nil && Date() - beaconLastSeen > 30 {
    beaconLastSeen = nil
    // call didExitRegion logic here
    }
    }
    

您将在最后一次信标检测后 30 秒收到退出事件. 您将在第一次看到输入事件时获得一个输入事件。

编辑:这是目标C中的相同代码:

NSDate *beaconLastSeen = nil;
...
if (beacons.count > 0) {
if (beaconLastSeen == nil) {
// call didEnterRegion logic here
}
beaconLastSeen = [[NSDate alloc] init];
}
else {
if (beaconLastSeen != nil  && [[[NSDate alloc] init] timeIntervalSinceDate: beaconLastSeen] > 30 ) {
beaconLastSeen = nil;
// call didExitRegion logic here
}
}

相关内容

最新更新