简体   繁体   中英

iPhone app crashes when asking for Location Services permission

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 !!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM