在Facebook / Twitter上使用SLComposeViewController发布多个图像?

时间:2014-10-28 10:32:36

标签: ios objective-c iphone ios7 ios5

我是iOS开发人员,目前我正在使用SLComposeViewController在Facebook / Twitter上分享帖子。我的问题是我必须在一个帖子中发布多个图像。

我这样做了如下:

SLComposeViewController* mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[mySLComposerSheet setInitialText:textTobeShared];
mySLComposerSheet addURL:[NSURL URLWithString:@"http://click-labs.com/"]];
for(int count=0;count<imageArray.count;count++)
        if([mySLComposerSheet addImage:[UIImage imageWithData:[imageArray objectAtIndex:count]]]) 

在上面的代码中,imageArray是我要发布的图像数组。

当我在Facebook上这样做时,所有图像都会作为单独的帖子发布。

在Twitter的情况下, addImage 方法仅对第一个图像返回true,而在其他图像的情况下,它返回false。因此只发布一张图片。

所以我想知道如何实现我的目标,是否可以在一条推文中发布多个图像。

1 个答案:

答案 0 :(得分:0)

我认为你需要先制作一张专辑。

这是link to the facebook album API文档。

- (void)shareToFacebook {
if (FBSession.activeSession.isOpen) {
    NSLog(@"SESSION IS OPEN");

    [self createFacebookAlbum];
} else {
    NSLog(@"SESSION IS NOT OPEN");
    NSArray* permissions = [NSArray arrayWithObject:@"email"];
    [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
        [self sessionStateChanged:session state:state error:error];

        if (error) {
            /* handle failure */
            NSLog(@"error:%@, %@", error, [error localizedDescription]);
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was a problem with your Facebook permissions." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
        else if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed ) {
            [FBSession.activeSession closeAndClearTokenInformation];
        }
        else if (state == FBSessionStateOpenTokenExtended || state == FBSessionStateOpen) {
            if(!self.presentedFacebookSheet) {
                [self performSelector:@selector(reauthorizeAndContinuePostToFacebook) withObject:nil afterDelay:0.5];
                self.presentedFacebookSheet = YES;
            }
        }
        }];
    }
}

- (void)reauthorizeAndContinuePostToFacebook {
    NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", nil];

    [[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) {
        [self shareToFacebook];
    }];
}

- (void)createFacebookAlbum {
    NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"Test name" forKey:@"name"];
    [parameters setObject:@"Test message" forKey:@"message"];

    FBRequest* request = [FBRequest requestWithGraphPath:@"me/albums" parameters:parameters HTTPMethod:@"POST"];

    NSLog(@"creating facebook album");
    FBRequestConnection *connection = [[FBRequestConnection alloc] init];
    [connection addRequest:request
         completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSString* albumId = [result objectForKey:@"id"];
                 NSLog(@"OK %@", albumId);
             }
             else {
                 NSLog(@"Error: %@",error.userInfo);
             }
         }];
    [connection start];
}

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error {
    switch (state) {
        case FBSessionStateOpen:
        {
            [[FBRequest requestForMe] startWithCompletionHandler:
             ^(FBRequestConnection *connection,
               NSDictionary<FBGraphUser> *user,
               NSError *error) {
                 if (error) {
                     //error
                 }
                 else {
                     NSLog(@"User session found");
                 }
             }];
        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }
}