Вопрос

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?

Это было полезно?

Решение

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

Другие советы

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top