Reputation: 1309
My app has a button, which, when tapped, gets the phone's current location and appends the CLLocation object's timestamp (NSString) to another string.
the first time the user uses the app and calls this method, the app crashes:
'NSInvalidArgumentException', reason: '* -[__NSCFConstantString sringByAppendingString:]: nil argument'
which I can understand, because the app doesn't have permission to use Location services yet and the timestamp is still nil.
At same time, after the app has crashed, the user is asked for permission, and this problem disappears once the user gives the app permission to use Location services.
How can I stop the app from crashing the first time ? i've tried to get the current Location in viewWillLoad if [locationManager authorizationStatus] != authorised, but the notification asking for permission only appears instantly.
Thanks for helping !!
Upvotes: 0
Views: 815
Reputation: 6353
To make sure you are using a nil
when there is risk of raising an exception you can check for nullity very simply:
if(stringToAppend) {
// Do something with the string
}
That way if the object is nil, the risky code won't be executed and the app won't crash.
Upvotes: 1