阅读 big nerd ranch ios 编程书籍并时不时遇到问题,因为这本书是基于 ios6 的编码,而我使用的是最新版本的 XCode/ios7。
无论如何,目前正在使用核心位置框架并将其添加到我的目标列表中。目前正在尝试获取具有英国伦敦模拟位置的设备的位置。这是我的代码,它有时有效,有时如果无法获取位置会引发错误。
.h 接口文件:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface WhereamiViewController : UIViewController
{
CLLocationManager *locationManager;
}
@end
.m 实现文件:
#import "WhereamiViewController.h"
@interface WhereamiViewController ()
@end
@implementation WhereamiViewController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self){
//create location manager object
locationManager = [[CLLocationManager alloc] init];
//there will be a warning from this line of code
[locationManager setDelegate:self];
//and we want it to be as accurate as possible
//regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//tell our manager to start looking for its location immediately
[locationManager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Could not find location: %@", error);
}
@end
控制台日志:
2013-10-13 19:21:43.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:43 PM British Summer Time
2013-10-13 19:21:44.545 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:44 PM British Summer Time
2013-10-13 19:21:45.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:45 PM British Summer Time
2013-10-13 19:21:46.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:46 PM British Summer Time
2013-10-13 19:21:47.547 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:47 PM British Summer Time
2013-10-13 19:21:48.548 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:48 PM British Summer Time
2013-10-13 19:21:48.925 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:48 PM British Summer Time
2013-10-13 19:21:49.926 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @
如您所见,有一个无限循环。处理这种情况的最佳方法是什么?我还在学习,不想让事情复杂化。我想跟上这本书的步伐。我明白我遇到的这些小问题对我的学习很有帮助。尝试理解做旧事情的新方法让我意识到有时我必须回到旧方法并完全理解它,然后才能用新方法做。
帮助将不胜感激问候。