1

I am trying to share data with facebook. I am able to share data except image stored locally in iPhone memory. But when I try to share image stored locally with parameter @"source" my app gets terminated with error "[NSConcreteMutableData _fastCharacterContents]".

For sharing a local image I am converting it into NSData as required by @"source" parameter.

NSData *data = [NSData dataWithContentsOfURL:localUrl];

   __block ACAccount *facebookAccount = nil;

    ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    // Specify App ID and permissions
    NSDictionary *options = @{
                              ACFacebookAppIdKey: @"xxxxxxxxxxxxxx",

                              ACFacebookPermissionsKey: @[@"email"],
                              ACFacebookAudienceKey: ACFacebookAudienceEveryone
                              }; // basic read permissions

    [self.accountStore requestAccessToAccountsWithType:facebookAccountType
                                          options:options completion:^(BOOL granted, NSError *e)
     {
         if (granted) {
             // Now that you have publish permissions execute the request
             NSDictionary *options2 = @{
                                        ACFacebookAppIdKey: @"xxxxxxxxxxxxxx",
                                        ACFacebookPermissionsKey: @[@"publish_stream", @"publish_actions"],
                                        ACFacebookAudienceKey: ACFacebookAudienceFriends
                                        };
             [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options2 completion:^(BOOL granted, NSError *error) {
                 if (granted) {
                     NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];

                     facebookAccount = [accounts lastObject];

                     /*NSDictionary *parameters = @{@"message": @"This is a test",
                                                  @"name": @"Sharing Tutorial",
                                                  @"caption": @"Build great social apps and get more installs.",
                                                  @"description": @"Allow your users to share stories on Facebook from your app using the iOS SDK.",
                                                  @"link": @"https://developers.facebook.com/docs/ios/share/",
                                                  @"source":data};*/
                     NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                    @"My hat image", @"message", data, @"source", nil];

                     NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];

                     SLRequest *feedRequest = [SLRequest
                                               requestForServiceType:SLServiceTypeFacebook
                                               requestMethod:SLRequestMethodPOST
                                               URL:feedURL
                                               parameters:parameters];
                     NSLog(@"AnythingHere?");

                     [feedRequest setAccount:facebookAccount];

                     [feedRequest performRequestWithHandler:^(NSData *responseData,
                                                              NSHTTPURLResponse *urlResponse, NSError *error)
                      {
                          // Handle response
                          NSLog(@"%@%@", error,urlResponse);

                          NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

                          NSLog(@"Facebook Response : %@",response);

                      }];



                 }
                 else {
                     NSLog(@"Access denied 2");
                     NSLog(@"%@", [error description]);
                 }
             }];
         } else {
             NSLog(@"Error: %@", [e description]);
             NSLog(@"Access denied");
         }
     }];

What can be the reason behind this error?

ViruMax
  • 1,216
  • 3
  • 16
  • 41
  • Please check this if it can help you http://code.google.com/p/touchcode/issues/detail?id=73 – iEinstein Feb 06 '14 at 07:01
  • check data's class `NSLog(@"class = %@", [data class]);` – Samet DEDE Feb 06 '14 at 07:04
  • @MatthiasBauch I am using data because url is not working [look here](http://stackoverflow.com/questions/14748140/share-image-from-bundle-and-link-on-facebook-in-iphone) – ViruMax Feb 06 '14 at 07:06
  • @EPyLEpSY data's class is NSConcreteMutableData – ViruMax Feb 06 '14 at 07:09
  • Perhaps you can share the full details of the exception, not just the method name in which you got the exception. And, if possible identify line that caused the error (sometimes an Xcode exception breakpoint can quickly narrow this down). But `NSConcreteMutableData` is just the internal representation when you create a `NSMutableData`. (`NSData` is [class cluster](https://developer.apple.com/library/mac/documentation/General/Conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html).) – Rob Feb 06 '14 at 07:10
  • 2
    check out [this](http://stackoverflow.com/a/12549033/1039901). – Samet DEDE Feb 06 '14 at 07:13
  • Thanks everybody, I got my answer. – ViruMax Feb 06 '14 at 07:28

2 Answers2

1

This is just an educated guess because I've never used the Social framework, but it's too long for a comment.

Judging by this answer I would say that you are using SLRequest wrong. I'm pretty sure that the exception comes from your data object which is interpreted as a NSString (or NSURL).
Maybe you should use addMultipartData:withName:type:filename: to attach your photo to the request.

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                      @"My hat image", @"message", nil];

NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];

SLRequest *feedRequest = [SLRequest
              requestForServiceType:SLServiceTypeFacebook
              requestMethod:SLRequestMethodPOST
              URL:feedURL
              parameters:parameters];

[feedRequest addMultipartData:data
                     withName:@"source"
                         type:@"multipart/form-data"
                     filename:@"Test Image"];

which is basically the same code as another answer

Community
  • 1
  • 1
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • you are GENIUS, thank you very much, that solved my problem, But I also tried the previous way with string as "source" parameter but it returns response saying "wrong format of url". Can u tell me why so? – ViruMax Feb 06 '14 at 07:26
  • you have to look at Facebooks documentation to find out what url format they expect. They might require a URL to a facebook post or something like that. As I said, I have never used Social.framework, and parameter format guessing is beyond my skills ;-) – Matthias Bauch Feb 06 '14 at 07:33
0

please recheck your parameters formation.

and convert your image in following formate(Base64)

NSData *rep = UIImagePNGRepresentation(img);
NSLog(@"Rep: %@", rep);
NSString *base64 = [rep encodeBase64];
NSLog(@"Base 64 is a %@", NSStringFromClass([base64 class]));
self.somestring = [@"hardcodedstring" stringByAppendingString:base64]; //encodeBase64 encodes it to base64
Mani
  • 305
  • 2
  • 9