位置经理始终在工作但不在使用时工作

问题描述 投票:1回答:1

在Apple坚持让用户在“始终”和“何时使用”位置管理器之间进行选择之前,我的应用程序运行良好。该应用程序使用iBeacons发送邀请来玩游戏并接受。 当选择“始终”时,信标工作正常,但当我切换到“何时使用”时,它们根本不起作用。我开始使用“始终”,但更改以下代码以便用户选择。在应用程序的plist中,我添加了“隐私 - 始终和使用时的位置使用说明和隐私 - 使用时的位置使用说明”,并删除了“隐私 - 位置始终使用说明”。

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

- (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是否需要将“隐私 - 位置”设置为“始终”才能工作?

所以我刚刚发现,在“何时使用”中你无法监视进入或退出的信标区域只能找到它的范围。所以我想问题是如何使用range向我的用户发送通知。

objective-c xcode cllocationmanager ibeacon
1个回答
0
投票

当您的应用程序被授权仅在前景中进行信标测距时,只需使用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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.