我一直在尝试创建一个允许用户使用核心位置框架检索当前位置的应用。我希望能够在应用程序启动时获取正在使用该应用程序的设备的坐标,并能够使用UILabel显示它们。有人可以告诉我我做错了什么或者我应该对代码做出哪些调整?
标题文件:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *longitude;
@property (strong, nonatomic) IBOutlet UILabel *latitude;
@property (strong, nonatomic) IBOutlet UILabel *altitude;
@property (strong, nonatomic) IBOutlet UILabel *speed;
@end
实施档案:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
CLLocationManager *locationManager;
[super viewDidLoad];
locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
locationManager.delegate = self; // I set the delegate of locationManager to self.
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // setting the accuracy
[locationManager startUpdatingLocation]; //requesting location updates
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an error retrieving your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[errorAlert show];
NSLog(@"Error: %@",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *crnLoc = [locations lastObject];
_latitude.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude];
_longitude.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
_altitude.text = [NSString stringWithFormat:@"%.0f m",crnLoc.altitude];
_speed.text = [NSString stringWithFormat:@"%.1f m/s", crnLoc.speed];
}
@end
我收到了这两个错误。
Assigning to 'id<CLLocationManagerDelegate> _Nullable' from incompatible type 'ViewController *const __strong'
'UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
答案 0 :(得分:2)
您没有实施CLLocationManagerDelegate
协议,这就是您收到第一个错误的原因。你可以像这样实现它:
@interface ViewController () <CLLocationManagerDelegate>
@end
您还应该检查documentation以了解您需要实施的方法,以便符合该协议,您必须在@implementation
中写入该协议。只有在符合该协议后才允许此分配:
locationManager.delegate = self; // I set the delegate of locationManager to self.
第二个问题似乎是一个警告。 Apple确实在iOS 9.0中弃用了UIAlertView
,它已被UIAlertController
取代,因此您应该使用它。 This NSHipster的文章解释了如何使用并将其与旧的,已弃用的版本进行比较,这很有趣,但它在Swift中(我将它留给他们提出的讨论)。 Here您还可以在Objective-C中找到一些示例。这是最后一个链接上显示的示例:
UIAlertController * view= [UIAlertController
alertControllerWithTitle:@"My Title"
message:@"Select you Choice"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Do some thing here
[view dismissViewControllerAnimated:YES
completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[view dismissViewControllerAnimated:YES completion:nil];
}];
[view addAction:ok];
[view addAction:cancel];
[self presentViewController:view animated:YES completion:nil];