1

As it's in tittle written, my app crashes immediately after it's startup. I really don't know, what to do. Thanks for any help. Here is code:

#import "ViewController.h"
@import CoreLocation;

@interface ViewController () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UILabel *location;
@property (weak, nonatomic) IBOutlet UILabel *date;
@end

@implementation ViewController
-(void)viewDidLoad
{
    [super viewDidLoad];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

    [self.locationManager startUpdatingLocation];

    //date
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"MMMM d, YYYY"];

    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"%@", dateString);
    self.date.text = (@"%@", dateString);
}

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

@end

After crash it shows this: https://i.stack.imgur.com/k0eVO.png

luk2302
  • 55,258
  • 23
  • 97
  • 137

2 Answers2

0

Your code is not complete can you check it out and post once again

You have got

@selector(requestWhenInUseAuthorization) 

and you have not set function requestWhenInUseAuthorization

Inside which method are you placing this below code. You have placed it below viewDidLoad but it isnot inside any methods. Check your code properly

[self.locationManager startUpdatingLocation];

 //date
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

 [dateFormatter setDateFormat:@"MMMM d, YYYY"];

 NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
 NSLog(@"%@", dateString);
 self.date.text = (@"%@", dateString);
}
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
  • I was confused as well, but it was just a formatting issue, the code is actually right and the regarding the selector: he has not to define it, he is calling it on the CLLocationManager, not on self – luk2302 Apr 05 '15 at 11:25
  • Everything worked fine, but after adding `self.date.text = (@"%@", dateString);` line it didn't work anymore. I tried to delete that line and ran again, but nothing worked. – pettyBright Apr 05 '15 at 11:41
  • Please delete this answer since it does not contain any relevant content – luk2302 Apr 05 '15 at 11:48
0

apply exception breakpoint in your app and try again .It will tell you where exactly the crash is

Arun Yadav
  • 203
  • 3
  • 9