我正在尝试与Facebook分享数据。我可以共享数据,除了本地存储在iPhone内存中的图像。但是当我尝试与参数@“source”共享本地存储的图像时,我的应用程序因错误“[NSConcreteMutableData _fastCharacterContents]”而终止。
为了共享本地图像,我按照@“source”参数的要求将其转换为NSData。
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");
}
}];
这个错误背后的原因是什么?
答案 0 :(得分:1)
这只是一个有根据的猜测,因为我从未使用过社交框架,但是评论太长了。
从这个answer判断我会说你使用SLRequest
错了。我很确定异常来自您的数据对象,它被解释为NSString(或NSURL)
也许您应该使用addMultipartData:withName:type:filename:
将照片附加到请求中。
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"];
的代码基本相同
答案 1 :(得分:0)
请重新检查您的参数形成。
并将您的图像转换为以下格式(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