0
votes

I am trying to post to Facebook from my iPhone app and it just doesn't work.

This is what I am doing:

if (![_facebook isSessionValid])
    {
        NSArray *permissions =  [NSArray arrayWithObjects:
                                 @"read_stream", @"publish_stream", @"offline_access",nil];

        [facebook authorize:permissions delegate:self];
    }

    NSMutableDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"User Prompt Message",  @"user_message_prompt",
                            @"http://www.mywebsite.com/", @"link",
                            @"http://mywebsite.com/wp-content/uploads/2011/05/iTunesArtwork.png", @"picture",
                            nil];


    [facebook requestWithGraphPath:@"me/feed"
                         andParams:params  
                     andHttpMethod:@"POST" 
                       andDelegate:self];

I also tried

[self.facebook dialog:@"feed" andParams:params andDelegate:self];

but in either case my app is getting terminated either in

  • (FBRequest*)openUrl:(NSString *)url params:(NSMutableDictionary *)params httpMethod:(NSString *)httpMethod delegate:(id)delegate

or in

  • (void)dialog:(NSString *)action andParams:(NSMutableDictionary *)params andDelegate:(id )delegate

depending on which method I call with this log:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

It doesn't make sense to me because the dictionaries being used are mutable.

Am I doing something wrong? Any help would be much appreciated.

1

1 Answers

2
votes

It doesn't make sense to me because the dictionaries being used are mutable.

No, they are not. Despite that you are assigning the returned object to a NSMutableDictionary variable, you are creating a NSDictionary here:

NSMutableDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                                    ^
                                  Here!
                              @"User Prompt Message",  @"user_message_prompt",
                              @"http://www.mywebsite.com/", @"link",
                              @"http://mywebsite.com/wp-content/uploads/2011/05/iTunesArtwork.png", @"picture",
                              nil];

This should be:

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:...];

This is likely the cause of the crash:

  • Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

because the Facebook API expects a mutable dictionary:

- (FBRequest*)requestWithGraphPath:(NSString *)graphPath
                         andParams:(NSMutableDictionary *)params
                                            ^
                                 It needs to be mutable!
                     andHttpMethod:(NSString *)httpMethod
                       andDelegate:(id <FBRequestDelegate>)delegate;