如何在tvOS

时间:2015-11-06 03:37:26

标签: objective-c tvos apple-tv

我有以下代码运行但总是调用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代码= 0&#34;(null)&#34;

修改 我还将NSLocationWhenInUseUsageDescription添加到.plist并添加了

[self.locationManager requestWhenInUseAuthorization];

取代[self.locationManager requestLocation]以上。我添加了didChangeAuthorizationStatus委托方法,并将调用移至requestLocation。在执行此操作之后,调用了状态值为kCLAuthorizationStatusNotDetermined的调用授权状态,并且没有出现任何权限请求,要求&#34;不允许&#34;或者&#34;允许&#34;以及之后对requestLocation的调用仍然触发didFailWithError。我尝试完全从Apple TV删除应用程序,认为这可能是一次性弹出,但requestWhenInUseAuthorization仍然没有做任何事情来提示许可。

我终于能够成功地让设备位置工作,只有在手动进入Apple TV设置 - 常规 - 隐私 - 位置服务,然后找到我的应用程序并手动更改设置从&#34;从不&#34;到#34;使用App&#34;。这显然不理想,如果requestWhenInUseAuthorization实际上会提示用户选择会更好。

1 个答案:

答案 0 :(得分:3)

完全基于发布的代码,您不会在执行此操作之前请求授权使用位置服务。您需要在CLLocationManager个实例上致电authorizationStatus。在尝试使用locationServicesEnabled和{{1}}之前调用{{1}}和{{1}}通常也是明智之举。你可以read more in the docs