gpt4 book ai didi

ios - CLLocationManager startUpdatingLocation 未调用 locationManager :didUpdateLocations: or locationManager:didFailWithError:

转载 作者:IT老高 更新时间:2023-10-28 11:47:22 40 4
gpt4 key购买 nike

我试图在我的 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

尽管文档中说了什么,但都没有调用委托(delegate)方法:

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

不知道如何调试问题。

谢谢,

JA

最佳答案

enter image description here只需将其添加到 info.plist 中

NSLocationAlwaysUsageDescription --- 我需要位置

NSLocationWhenInUseUsageDescription --- 我需要位置

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

请注意,应更改“我需要位置”以描述您实际应用的设计用途。它在授权消息中传达给最终用户。 (感谢@devios1)

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

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

更多详情click here

关于ios - CLLocationManager startUpdatingLocation 未调用 locationManager :didUpdateLocations: or locationManager:didFailWithError:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25916841/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com