我正在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。核心位置在Xcode 7.3中不起作用
我有「NSLocationWhenInUseUsageDescription」添加到plist值爲「請求位置」。
我很困惑,爲什麼這不起作用。我沒有得到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
什麼是錯的我們的代碼?是地點零? –
你將需要調試你的代碼。在調用requestWhenInUseAuthorization之前和之後檢查authorizationStatus的值。同時實現委託方法'locationManager:didChangeAuthorizationStatus:'來查看授權狀態的變化。 –
@DuncanC我將我的.m更改爲以下內容 - [鏈接](http://i.imgur。com/m3quLKk.png),並用調試器遍歷它。當[self.locationManager requestWhenInUseAuthorization];被稱爲沒有發生在應用程序中。我也嘗試了[經理requestWhenInUseAuthorization]並沒有得到任何東西。有關如何解決這個問題的任何提示? – Aranelmalina