基于The Big Nerd Ranch Guide学习IOS7的CoreLocation

时间:2014-01-25 00:16:46

标签: ios7 xcode5 cllocationmanager

我正在学习带有xcode 5的IOS7的coreLocation。我参考了大书呆子牧场指南版本3.我正在使用iphone视网膜(3.5英寸)模拟器。

我在第4章,从CLLocationManager检索更新(即:whereami)。本主题的目的是了解CLLocationManager,委托和检索当前位置。

我被卡住,因为当我运行模拟器时,我应该在控制台中看到对象的位置,看起来像这样。 :

<+37.33168900, -122.03073100> +/- 100.00m (speed -1.00 mps / course -1.00)

我没有得到上述结果。我已将模拟位置设置为伦敦,英格兰。在书上它说我应该给予应用程序在设备上使用位置服务的一些许可,但我没有得到任何在模拟器上。我还注意到在日志导航器下,Debug仍在加载。在Debug导航器中,内存不断增加。我尝试在WhereamiViewController.m中设置断点,但程序无法到达此处的任何位置。然后我在main.m中设置并尝试在下面的代码行中进行一步,但它刚刚结束:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([WhereamiAppDelegate class]));

这是我的代码:

WhereamiViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}

@end

WhereamiViewController.m

#import "WhereamiViewController.h"

@implementation WhereamiViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        // Create location manager object
        locationManager = [[CLLocationManager alloc] init];

        // There will be a warning from this line of code; ignore it for now
        [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 immeidately
        [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 locatoin:%@", error);
}

-(void)dealloc
{
    // Tell the location manager to stop sending us messages
    [locationManager setDelegate:nil];
}
@end

的main.m

#import <UIKit/UIKit.h>

#import "WhereamiAppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([WhereamiAppDelegate class]));
    }
}

我是非常新的,所以任何帮助指出我应该采取的正确方向将非常感激=)

1 个答案:

答案 0 :(得分:0)

我在http://forums.bignerdranch.com/viewtopic.php?f=216&t=4514#p20666

找到了解决方案

只有我看起来更加努力。

Xcode5使用故事板。所以它不会调用initWithNib。为了让它工作,我用以下

替换了(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:(NSCoder *)aDecoder];

    if (self) {
        locationManager = [[CLLocationManager alloc] init];

        [locationManager setDelegate:self];

        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        [locationManager startUpdatingLocation];
    }
    return self;
}

在iOS7中,didUpdateToLocation:fromLocation已弃用,并替换为didUpdateLocations。替换为以下内容:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"%@",[locations lastObject]);
}

我测试了它现在正在工作。