[英]How to call the CLLocation Manager delegate methods in iOS7 using swift
[英]CLLocation calling delegate methods
我試圖在切換到特定視圖控制器(FeedVC)時獲取用戶位置,這是我當前擁有的代碼:
FeedVC.h
#import <CoreLocation/CoreLocation.h>
@interface FeedVC : UIViewController <CLLocationManagerDelegate>
FeedVC.m
@implementation FeedVC
{
CLLocationManager *locationManager;
CLLocation *location;
}
- (void)viewDidLoad {
[super viewDidLoad];
if (!locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"did update locations");
location = [locations lastObject];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
-(void) viewWillAppear:(BOOL)animated
{
NSLog(@"%.2f", location.coordinate.latitude);
}
方法locationManager: didUpdateLocations:
沒有被調用(因為未顯示NSLog)我已經在這里關注所有文檔和其他答案,但是我找不到問題的根源,謝謝!
不要調用[locationManager startUpdatingLocation];
在viewDidLoad中,在委托方法中調用它:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[locationManager startUpdatingLocation];
}
}
如果尚未在plist中添加NSLocationWhenInUseUsageDescription,請添加它以描述為什么要使用位置服務
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.