Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

MKMApView Problems

I'm having trouble in Xcode retrieving the user's location. I will post my code from my header and implementation file. My code is completely flawless and there are no compiler errors. in my main stroryboard i have a map view control, segmented control with the three buttons:Hybrid, Satellite, and Map. (they all work fine). On the same navigation bar. I have a button named getLocation but when I press it in the simulator my app crashes and i get The UIApplicationMain error.

import <UIKit/UIKit.h>

import <MapKit/MapKit.h>

@interface ViewController : UIViewController <MKMapViewDelegate> { MKMapView *mapView;

} @property (strong, nonatomic) IBOutlet MKMapView *mapView; -(IBAction)setMap:(id)sender;

-(IBAction)getLocation:(id)sender;

@end

and here is my implementation file.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController @synthesize mapView;

-(IBAction)getLocation:(id)sender { mapView.showsUserLocation = YES; }

-(IBAction)setMap:(id)sender { switch (((UISegmentedControl *) sender) .selectedSegmentIndex) { case 0: mapView.mapType = MKMapTypeStandard; break; case 1: mapView.mapType = MKMapTypeSatellite; break; case 2: mapView.mapType = MKMapTypeHybrid; break; default: break; } }

  • (void)viewDidLoad { [super viewDidLoad]; [self.mapView.delegate self]; [self.mapView setShowsUserLocation: YES]; // Do any additional setup after loading the view, typically from a nib. }

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{ CLLocationCoordinate2D loc = [userLocation coordinate];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
[self.mapView setRegion:region animated:YES];

any help or tips would be helpful. Im open to all ideas.

1 Answer

Chris McKnight
PLUS
Chris McKnight
Courses Plus Student 11,045 Points

Turn on the exception breakpoint by going to the breakpoint navigator (second to last tab on the left pane), then clicking +, and clicking Add Exception Breakpoint. The debugger will pause on a line, if possible, when an exception occurs.

Look in the console and see if you see an error message. I have noticed the debugger sometimes catches the exception and you have hit continue a few times which crashes the app and shows the error message in the console.

In my experience, a crash like this is more than likely caused by either the method that is being called doesn't exist or there is a action/outlet in a storyboard/nib that is leftover from a refactor. The missing action/outlet causes the app to crash.