我正在Objective-C中为iphone 9.3.3开发Xcode 7.3中的应用程序。我按照本网站上的建议 - http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/获取了iPhone的位置。我正在测试我到目前为止使用iphone 9.3.3通过闪电电缆。
我将“NSLocationWhenInUseUsageDescription”添加到plist,其值为“Request Location”。
我很困惑为什么这不起作用。我没有获得NSLog上的位置任何字符串。
非常感谢任何帮助。
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (weak, nonatomic) CLLocationManager *locations;
//
//@property (weak, nonatomic) IBOutlet UILabel *longitudelabel;
//@property (weak, nonatomic) IBOutlet UILabel *latitudelabel;
@end
ViewController.m
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ViewController.h"
@interface ViewController ()
{
NSUserDefaults *defaults;
}
@end
@implementation PollenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// ** Don't forget to add NSLocationWhenInUseUsageDescription in MyApp-Info.plist and give it a string
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
// Location Manager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@", [locations lastObject]);
}
@end
答案 0 :(得分:0)
对于iOS 8,请求权限并开始使用位置服务现在是单独的操作。从那以后
requestWhenInUseAuthorization
和
requestAlwaysAuthorization
方法异步运行,应用程序无法立即启动位置服务。相反,您需要实现
locationManager:didChangeAuthorizationStatus
委托方法如下,根据用户输入随时激活授权状态。
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedAlways
|| status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[manager startUpdatingLocation];
}
}
如果用户之前已授予使用位置服务的权限,则在初始化位置管理器并将其委托设置为具有相应授权状态的情况下,也将调用委托方法,如下所示
CLLocationManager *manager = [CLLocationManager new]
Manager.delegate = self;
if [CLLocationManager locationServicesEnabled] {
[manager startUpdatingLocation];
}
答案 1 :(得分:0)
我已经能够获取位置并使用以下ViewController.m代码在应用程序中显示它。 @DuncanC的评论有很多帮助。这还包括反向地理编码。
- 使用字符串“需要位置”的Info.plists中的NSLocationWhenInUseUsageDescription。 - 确保设置 - &gt;隐私 - &gt;位置服务 - &gt; AppName设置为“使用时”而不是“从不”。
希望这对任何被困的人都有帮助。
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "CoreLocation/CoreLocation.h"
@interface ViewController ()
{
NSUserDefaults *defaults;
NSString *locationfound;
}
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation PollenViewController {
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//implement location finder
self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
//initialize geocoder for later
geocoder = [[CLGeocoder alloc] init];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
//CURRENTLY WORKING LOCATION MANAGER
//roughly based on ---/*https://gist.github.com/paulw11/84b18044942e2ceea52a8*/
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:
(CLAuthorizationStatus)status {
defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"Callback");
NSLog(@"status %i",status);
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
NSLog(@"Authorized");
[self.locationManager startUpdatingLocation];
} else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {
NSLog(@"Denied");
locationfound=[NSString stringWithFormat:@"No Location Found"];;
self.LocationLabel.text=locationfound;
}else{
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
}
// Location Manager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
NSLog(@"Location new %@",newLocation) ;
// Reverse Geocoding
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
defaults = [NSUserDefaults standardUserDefaults];
// NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
locationfound= [NSString stringWithFormat:@"%@, %@",
placemark.locality,
placemark.administrativeArea];
NSLog(@"%@",locationfound);
self.LocationLabel.text=locationfound;
} else {
NSLog(@"%@", error.debugDescription);
locationfound=[NSString stringWithFormat:@"No Location Found"];;
self.LocationLabel.text=locationfound;
}
} ];
}
@end
答案 2 :(得分:0)
你可以尝试
- (void)locationManager:(CLLocationManager*) manager didFailWithError:(NSError*)error
查看更新是否失败以及原因