Search code examples
swiftfacebook-graph-apifacebook-ios-sdk

NSInvalidArgumentException with FBSDKGraphRequest


I'm getting the following exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary imagePathForPictureMode:size:]: unrecognized selector sent to instance 0x7ff9f8d25d10'

with this code:

PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions, block: {
    (user: PFUser?, error: NSError?) -> Void in

    if let user = user {
        if user.isNew {
            var request = FBSDKGraphRequest(graphPath: "me", parameters: nil)
            request.startWithCompletionHandler({
                (connection, result, error) -> Void in

                if error == nil {
                    // exception thrown next line
                    let strProfilePicURL = result.imagePathForPictureMode(FBSDKProfilePictureMode.Square, size: self.profileImage.frame.size)
                    let url = NSURL(string: strProfilePicURL, relativeToURL: NSURL(string: "http://graph.facebook.com/"))
                    let imageData = NSData(contentsOfURL: url!)
                    let image = UIImage(data: imageData!)

                    self.profileImage.image = image!
                }
            })
            println("User signed up and logged in through Facebook!")
        } else {
            println("User logged in through Facebook!")
        }
    } else {
        println("Uh oh. The user cancelled the Facebook login.")
    }
})

Why is it throwing an exception here? I've also tried:

let size = self.profileImage.frame.size as CGSize
let strProfilePicURL = result.imagePathForPictureMode(FBSDKProfilePictureMode.Square, size: size)

Solution

  • The currentProfile is fetched asynchronously after the SDK gets an access token, so depending on when you're asking for the currentProfile, it may be nil.

    You can also observe the FBSDKProfileDidChangeNotification for when the profile is updated. See https://developers.facebook.com/docs/reference/ios/current/class/FBSDKProfile/