2014-01-25 58 views
0

嗨im學習coreLocation IOS7與xcode 5. im引用到大書呆子牧場指南版本3.我使用iphone視網膜(3.5英寸)模擬器。學習基於大書呆子牧場指南IOS7的CoreLocation

我在第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])); 
    } 
} 

我在這所以在指出我應該採取的正確方向任何幫助將非常感激很新=)

+0

你能分享'AppDelegate.m'的代碼嗎? – Aaron

+0

嗨,我發現我的解決方案。我會在下面發佈 – nuttynibbles

回答

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]); 
} 

我測試過,現在正在工作。