evanhaus evanhaus - 11 months ago 90
Swift Question

(Swift)(Firebase) Getting EXC_BAD_INSTRUCTION with Facebook login

I am creating an app that uses Facebook to login and retrieves your profile picture to use as the profile picture for the app. This is the code:

if let user = FIRAuth.auth()?.currentUser {
// User is signed in.
let name = user.displayName
let email = user.email
let photoUrl = user.photoURL
let uid = user.uid;

self.profileName.text = name

// ERROR: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
let data = NSData(contentsOfURL: photoUrl!)
self.profilePic.image = UIImage(data: data!)


This error only happens when using login NOT through Facebook. When doing it through Facebook it works fine. I have a hunch that the error arises because if I log in regularly (not through Facebook) this line will say "there is no photo, what are you talking about?" and crash. However, I am unsure as to how to circumvent this issue. I am using Firebase.

Any help is greatly appreciated.


This means that photoUrl is nil. The code crashes because you're using the forced unwrap operator !. You should safely unwrap it instead:

if let photoUrl = photoUrl, data = NSData(contentsOfURL: photoUrl) {
    self.profilePic.image = UIImage(data: data)
else {
    print("No photo")