0

我想使用我的 AppID 在 facebook 上分享我的图像。所以我可以在 facebook 上看到它via App。它工作正常,直到 iOS 5。但不是在 iOS 6。

如果我使用的是SLComposeViewController,那么我可以共享图像,但显示的是“通过 iOS”而不是“通过应用程序”。所以我已经查看了它的代码并应用了它,但它没有共享图像。请检查代码是否有任何问题并进行更正。

facebookaccount = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];

// Specify App ID and permissions
NSDictionary *options = @{ ACFacebookAppIdKey: @"xxxxxxxxxxxxx", ACFacebookPermissionsKey: @[@"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
    if (granted) {
        NSArray *accountsArray = [facebookaccount accountsWithAccountType:facebookaccountType];
        facebookAccount = [[accountsArray lastObject] retain];
        NSLog(@" Account :::: %@", facebookAccount);
    } else {
        NSLog(@" Error :::: %@", error.description);
    }}];
NSDictionary *parameters = @{@"message" : @""};
NSString *link = [NSString stringWithFormat:@"%simages/%@",ServerPath,[images objectAtIndex:imageNO]];
NSURL *url = [NSURL URLWithString:link];
NSData *data = [NSData dataWithContentsOfURL:url];

CGFloat compression = 1.0f;
CGFloat maxCompression = 0.0f;
int maxFileSize = 150*224;
UIImage *img1  = [[UIImage alloc] initWithData:data];
NSData *imageData = UIImageJPEGRepresentation(img1, compression);

while ([imageData length] > maxFileSize && compression > maxCompression){
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation(img1, compression);
}

UIImage *img  = [[UIImage alloc] initWithData:imageData];
NSLog(@"Image ::: %@", img);
NSData *myImageData = UIImagePNGRepresentation(img);
NSLog(@" Image Data ::: %@", myImageData);
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:requestURL parameters:parameters];

[facebookRequest addMultipartData:myImageData withName:@"source" type:@"multipart/form-data" filename:nil];
NSLog(@"Request Account :::: %@", facebookAccount);
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    NSLog(@"Done with Facebook.......");
    if (!error) {
        NSDictionary *list = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSLog(@" Dictionary ::: %@", list);
    } else {
        NSLog(@" Error :::: %@", error.description);
    }}];

我在字典日志中得到空值。

可能是什么问题?谢谢你。

4

2 回答 2

5

使用下面的代码而不是 SLComposeViewController 使用 SLRequest

NSDictionary *parameters = @{@"message": sendmessage};

SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                        requestMethod:SLRequestMethodPOST
                                                  URL:[NSURL    URLWithString:@"https://graph.facebook.com/me/photos"]
                                                       parameters:parameters];

[facebookRequest addMultipartData: myImageData
                                 withName:@"source"
                                     type:@"multipart/form-data"
                                 filename:@"TestImage"];

facebookRequest.account = facebookAccount;

[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
 // Log the result
}];
于 2013-02-02T07:25:54.913 回答
1

我希望这也能帮助你

-(void)facebookPost:(NSString *)text{
     ACAccountStore *facebookaccount1 = [[ACAccountStore alloc] init];
     ACAccountType *facebookaccountType = [facebookaccount1 accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
    // Specify App ID and permissions
    NSDictionary *options = @{ ACFacebookAppIdKey: @"xxxxxxxxxxxxxxx", ACFacebookPermissionsKey: @[@"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
    [facebookaccount1 requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
    if(granted) {
        NSArray *accountsArray = [facebookaccount1 accountsWithAccountType:facebookaccountType];
        if ([accountsArray count] > 0)
        {
            NSDictionary *parameters = @{@"message": text};

            SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                            requestMethod:SLRequestMethodPOST
                                                                      URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"]
                                                               parameters:parameters];

            NSString *link = [NSString stringWithFormat:@"%simages/%@",ServerPath,[images objectAtIndex:imageNO]];  // image link
            NSURL *url = [NSURL URLWithString:link];
            NSData *data = [NSData dataWithContentsOfURL:url];

            // code for compress image you can delete this code if you dont want to compress image 
            CGFloat compression = 1.0f;
            CGFloat maxCompression = 0.0f;
            int maxFileSize = 150*224;
            UIImage *img1  = [[UIImage alloc] initWithData:data];
            NSData *imageData = UIImageJPEGRepresentation(img1, compression);

            while ([imageData length] > maxFileSize && compression > maxCompression)
            {
                compression -= 0.1;
                imageData = UIImageJPEGRepresentation(img1, compression);
            }

            // imageData is your image
            [facebookRequest addMultipartData: imageData
                                     withName:@"source"
                                         type:@"multipart/form-data"
                                     filename:@"TestImage"];
            ACAccount *facebookAccount1 = [accountsArray objectAtIndex:0];
            [facebookRequest setAccount:facebookAccount1];

            [facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
             {
                 // Log the result
             }];
        }
    }
}];


}
于 2013-02-05T12:38:48.450 回答