Ios Xcode 5 didupdatelocations不工作
我将按照本教程创建一个显示用户位置的程序。我已经告诉Xcode为我模拟一个位置,甚至在模拟器上也确保它允许我的应用程序跟踪位置。但是,没有任何内容被记录到我的控制台 头文件:Ios Xcode 5 didupdatelocations不工作,ios,objective-c,ios7,xcode5,xcode5.0.1,Ios,Objective C,Ios7,Xcode5,Xcode5.0.1,我将按照本教程创建一个显示用户位置的程序。我已经告诉Xcode为我模拟一个位置,甚至在模拟器上也确保它允许我的应用程序跟踪位置。但是,没有任何内容被记录到我的控制台 头文件: #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@end
我不是100%确定答案,但这就是我得到的答案。
iOS 7仅在应用程序位于前台时才允许位置提取。
当您在
initwithNibName
中编写代码时,您的应用程序实际上并不在前台,而是在从xib文件和所有文件创建控件。这就是为什么操作系统不向您提供位置更新。如果我将代码放入viewDidLoad中,则@josh可能会重复,但在initwithNibName中则不会。为什么呢?最初,initWithNibName甚至没有被调用,所以我在AppDelegate.mwheremiViewController*wvc=[[wheremiViewController alloc]initWithNibName:@“wheremiViewController”捆绑包中设置它:nil]代码>这是为我做的。在应用程序的第一次测试中,我调用了startUpdatingLocation
,并使用选项完成了启动,这似乎为时过早。按一下按钮后,一切正常。
#import "WhereamiViewController.h"
@implementation WhereamiViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setPausesLocationUpdatesAutomatically:NO];
[locationManager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@", [locations lastObject]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", error);
}
@end