Objective-C 定位服务问题
Objective-C issues with location services
我正在尝试制作一个简单的应用程序,它将获得用户的经度和纬度
我按照这里的教程操作:
http://www.appcoda.com/how-to-get-current-location-iphone-user/
随之而来的是:
.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
- (IBAction)getCurrentLocation:(id)sender;
@end
.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
- (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];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
@end
我的问题是 "Would like to use your current location" 弹出窗口。
和 none 的委托方法都被命中了。我在每个委托方法的开头放置了一个断点,什么也没有。请帮忙。
请确保您的 info.plist
中有以下密钥:
NSLocationAlwaysUsageDescription
值为 I need Location
NSLocationWhenInUseUsageDescription
值为 I need Location
privacy - location usage description
值为 I need Location
确保 info.plist 详细信息如前所述,然后在初始化期间添加以下代码;
if (IS_OS_8_OR_LATER) {
[locationmanager requestWhenInUseAuthorization];
//or (as per your app requirement)
[locationmanager requestAlwaysAuthorization];
}
我正在尝试制作一个简单的应用程序,它将获得用户的经度和纬度
我按照这里的教程操作:
http://www.appcoda.com/how-to-get-current-location-iphone-user/
随之而来的是:
.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
- (IBAction)getCurrentLocation:(id)sender;
@end
.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
- (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];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
@end
我的问题是 "Would like to use your current location" 弹出窗口。
和 none 的委托方法都被命中了。我在每个委托方法的开头放置了一个断点,什么也没有。请帮忙。
请确保您的 info.plist
中有以下密钥:
NSLocationAlwaysUsageDescription
值为I need Location
NSLocationWhenInUseUsageDescription
值为I need Location
privacy - location usage description
值为I need Location
确保 info.plist 详细信息如前所述,然后在初始化期间添加以下代码;
if (IS_OS_8_OR_LATER) {
[locationmanager requestWhenInUseAuthorization];
//or (as per your app requirement)
[locationmanager requestAlwaysAuthorization];
}