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);
    }}];

ディクショナリ ログに null 値が記録されています。

何が問題になる可能性がありますか? ありがとうございました。

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 に答える