【问题标题】:How can I get the current location of an Apple TV device in tvOS如何在 tvOS 中获取 Apple TV 设备的当前位置
【发布时间】:2015-11-06 03:37:26
【问题描述】:

我有以下代码运行但总是调用 didFailWithError 委托方法。甚至可以在 tvOS 中获取 Apple TV 设备的位置吗?
AppDelegate.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) CLLocationManager *locationManager;

@end

AppDelegate.m

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSLog(@"locationManager requestLocation");
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self.locationManager requestLocation];

    return YES;
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"locationManager didFailWithError %@", error);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"locationManager didUpdateLocations");
}

控制台显示:
locationManager didFailWithError 错误域=kCLErrorDomain Code=0 "(null)"

编辑: 我还将 NSLocationWhenInUseUsageDescription 添加到 .plist 并添加了

[self.locationManager requestWhenInUseAuthorization];

代替上面的[self.locationManager requestLocation]。我添加了didChangeAuthorizationStatus 委托方法并将调用移至那里的requestLocation。执行此操作后,使用状态值 kCLAuthorizationStatusNotDetermined 调用了 didChangeAuthorizationStatus,并且没有弹出权限请求要求“不允许”或“允许”,随后对requestLocation 的调用仍然触发didFailWithError。我尝试从 Apple TV 中完全删除该应用程序,认为这可能是一次性弹出窗口,但 requestWhenInUseAuthorization 仍然没有做任何提示许可的操作。

只有在手动进入 Apple TV 设置 - 常规 - 隐私 - 定位服务,然后找到我的应用并手动将设置从“从不”更改为“使用应用程序”。这显然不理想,如果requestWhenInUseAuthorization 真的会提示用户选择会更好。

【问题讨论】:

标签: objective-c tvos apple-tv


【解决方案1】:

仅根据发布的代码,您在使用位置服务之前并未请求授权。您需要在您的 CLLocationManager 实例上调用 requestWhenInUseAuthorization。在尝试使用它之前调用authorizationStatuslocationServicesEnabled 通常也是明智的。你可以read more in the docs

【讨论】:

  • 我在上面添加了对原始问题的编辑,结果添加了 requestWhenInUseAuthorization、将调用移动到 requestLocation 以及将 NSLocationWhenInUseUsageDescription 添加到 .plist ...几乎是一个解决方案,但还没有完全解决。
  • 听起来 SDK 并不容易。至少一旦您调用请求身份验证,您就可以在设置中手动更改它。您是否尝试过完全删除您的应用并重新安装它以查看它是否第一次出现提示而您只是以某种方式错过了它?
  • 我确实尝试将其完全删除并重新安装,但仍然没有提示。我不记得它是否第一次提示我,我可能错过了。
  • @ScottNull 你有没有得到提示?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 2012-03-19
  • 2015-12-06
相关资源
最近更新 更多