App crashing with exception: -[NSConcreteMutableData _fastCharacterContents]

橙三吉。 提交于 2019-12-13 01:27:02

问题


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?


回答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




回答2:


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


来源:https://stackoverflow.com/questions/21596051/app-crashing-with-exception-nsconcretemutabledata-fastcharactercontents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!