gpt4 book ai didi

ios - didUpdateLocations 到 : from: method is deprecated for latest iOS versions(12. 4)。如何用 didUpdateLocations 替换它?

转载 作者:行者123 更新时间:2023-12-04 07:56:37 28 4
gpt4 key购买 nike

我正在尝试在我的示例应用程序中实现经度和纬度。我使用 iPhone 5s 设备进行测试。它不会对按钮操作的纬度和经度标签产生任何影响。
在编译 Xcode 之前,在 didUpdateToLocation: fromLocation: method as 行显示警告
“实现已弃用的方法”
请帮助我使用合适的方法来代替此方法以使应用程序顺利运行
使用 Xcode 12.4 和使用 objective-c 的 iOS 12.3 我试图实现一个简单的位置演示应用程序,它显示经度和纬度。下面是我的 viewController.h 和 viewController.m 文件。我已经尝试过以下来自在线教程的代码 AppCoda
viewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController :UIViewController<CLLocationManagerDelegate>
{
IBOutlet UILabel *lattitudeLabel;
IBOutlet UILabel *longitudeLabel;
IBOutlet UILabel *addressLabel;
}
- (IBAction)getCurrentLocation:(UIButton *)sender;
@end
viewController.m
#import "ViewController.h"

@interface ViewController ()
{
CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc]init];
geocoder = [[CLGeocoder alloc] init];
}


- (IBAction)getCurrentLocation:(UIButton *)sender
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
NSLog(@"Error: Failed to get location");
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
lattitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}

[locationManager stopUpdatingLocation];

NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
self->placemark = [placemarks lastObject];
self->addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
self->placemark.subThoroughfare, self->placemark.thoroughfare,
self->placemark.postalCode, self->placemark.locality,
self->placemark.administrativeArea,
self->placemark.country];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
@end
enter image description here
单击按钮获取我的位置没有任何事情发生。它实际上应该更新除了经度:和纬度:一些浮点值之外的坐标..但它没有这样做..我会问请给我建议任何教程网站或任何GitHub项目的链接,以便在iOS 12.3中进行位置处理和以上在 objective-c ...感谢您的答复和建议的编辑。

最佳答案

docs说,只需使用 locationManager(_:didUpdateLocations:) .
swift 5

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Stop updates if this is a one-time request
manager.stopUpdatingLocation()

// Pop the last location off if you just need current update
guard let newLocation = locations.last else { return }

<... Do something with the location ...>
}
Objective-C
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
[manager stopUpdatingLocation];

CLLocation *newLocation = [locations lastObject];
if (newLocation) {
<... Do something with the location ...>
}
}

关于ios - didUpdateLocations 到 : from: method is deprecated for latest iOS versions(12. 4)。如何用 didUpdateLocations 替换它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66673080/

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