我试图在iOS项目中使用CLLocationManager框架来访问用户的位置,但当我打电话时

[locationManager startUpdatingLocation]

locationManager:didUpdateLocations:locationManager:didFailWithError:都没有接到电话.

//myViewController.h
@interface myViewController : UITableViewController <CLLocationManagerDelegate>
@end

//myViewController.m
@implementation myViewController{
    CLLocationManager *locationManager;
}

//edit
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
}
//finish edit

-(void)getLocation
{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" 
                                 message:@"Failed to Get Your Location"              
                                delegate:nil 
                       cancelButtonTitle:@"OK" 
                      otherButtonTitles:nil];
[errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = locations[[locations count] -1];
    CLLocation *currentLocation = newLocation;
    NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];

if (currentLocation != nil) {
   NSLog(@"latitude: %@", latitude);
   NSLog(@"longitude: @"%@", longitude);
}else {
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" 
                                                     delegate:nil 
                                            cancelButtonTitle:@"OK" 
                                            otherButtonTitles:nil];
    [errorAlert show];
}

}
@end

不管文档中怎么说,都不会调用委托方法:

"此方法立即返回.调用此方法会导致位置管理器获得初始位置修复(可能需要几秒钟),并通过调用其locationManager:didUpdateLocations:方法[…]通知您的代理."除了实现locationManager:didUpdateLocations:方法的委托对象之外,它还应该实现locationManager:didFailWithError:方法来响应潜在的错误."

不知道如何调试这个问题.

谢谢

青年成就组织

推荐答案

在此处输入图像描述只需将其添加到info.plist中即可

NSLocationAlwaysUsageDescription--我需要位置

NSLocation WhenUsageDescription--我需要位置

隐私-位置使用说明-我需要位置

  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate=self;
  locationManager.desiredAccuracy=kCLLocationAccuracyBest;
  locationManager.distanceFilter=kCLDistanceFilterNone;
  [locationManager requestWhenInUseAuthorization];
  [locationManager startMonitoringSignificantLocationChanges];
  [locationManager startUpdatingLocation];

现在它肯定会调用你的didUpdateToLocation.

欲知更多详情,请点击click here

Objective-c相关问答推荐

iOS9 - 这个应用程序正在从后台线程修改自动布局引擎 - 在哪里?

为什么我不应该在 init/dealloc 中使用 Objective C 2.0 访问器?

Objective-C 中的正式协议和非正式协议有什么区别?

将 Objective-C 指针类型NSString *转换为 C 指针类型CFStringRef(又名const struct __CFString *)需要桥接转换

为什么我的 UIButton 的内部修饰事件没有被调用?

将长格式的 NSString 拆分为多行

UITextField 中的文本在编辑后向上移动(编辑时居中)

如何删除 xcode 5 中的旧配置文件?

NSString:从字符串中删除 UTF-8 重音的简单方法?

何时应该使用 initWithNibName 初始化视图控制器?

'supportedInterfaceOrientations' 实现中的返回类型冲突: - 警告

Cocoa 的依赖注入框架?

如何将 NSNumber 转换和比较为 BOOL?

在字符串Objective-c中连接字符串

Objective-C 的 NSMutableArray 是线程安全的吗?

UIWebView didFinishLoading 多次触发

使用 CocoaPods 时如何向 Xcode 添加自定义项目配置?

我可以在 Objective-C switch 语句中声明变量吗?

[NSMutableArray array] 与 [[NSMutableArray alloc] init] 之间的区别

哪个是正确的,nil 或 NULL,来标记没有 Objective-C 块?