Search code examples
iosobjective-cfacebookfacebook-sdk-4.0objective-c-category

Unrecognized selector sent to instance on FBSDKProfile category


I'm using the Facebook v4 SDK in my iOS app. To get relevant information, I frequently use the [FBSDKProfile currentProfile] singleton. However, I also need the profile image to be readily accessible, and hence wrote a category to take of this.

This is the header file:

#import <FBSDKCoreKit/FBSDKCoreKit.h>

@interface FBSDKProfile (ProfileImage)

+(void)fetchProfileImageWithBlock:(void (^)(BOOL succeeded))handler;

@property (nonatomic, strong, readonly) UIImage *profileImage;

@end

Here's the implementation file:

#import "FBSDKProfile+ProfileImage.h"

@interface FBSDKProfile()

@property (nonatomic, strong, readwrite) UIImage *profileImage;

@end

@implementation FBSDKProfile (ProfileImage)

+(void)fetchProfileImageWithBlock:(void (^)(BOOL succeeded))handler {
    FBSDKProfile *currentProfile = [FBSDKProfile currentProfile];
    NSString *userId = currentProfile.userID;
    if (![userId isEqualToString:@""] && userId != Nil)
    {
        [self downloadFacebookProfileImageWithId:userId completionBlock:^(BOOL succeeded, UIImage *profileImage) {
            currentProfile.profileImage = profileImage;
            if (handler) { handler(succeeded); }
        }];
    } else
    {
        /* no user id */
        if (handler) { handler(NO); }
    }
}

+(void)downloadFacebookProfileImageWithId:(NSString *)profileId completionBlock:(void (^)(BOOL succeeded, UIImage *profileImage))completionBlock
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", profileId]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               if (!error)
                               {
                                   UIImage *image = [[UIImage alloc] initWithData:data];
                                   completionBlock(YES, image);
                               } else{
                                   completionBlock(NO, nil);
                               }
                           }];
}

@end

However, I'm getting this exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FBSDKProfile setProfileImage:]: unrecognized selector sent to instance

Why is this?


Solution

  • Problem

    You have added the attribute readonly to your property profileImage. It means what it says :

    You can only read it, so calling the setter will throw an exception.

    Solution

    Don't assign readonly attribute to profileImage

    @property (nonatomic, strong) UIImage *profileImage;