[英]How to post multiple images using SLComposeViewController on Facebook and Twitter in same album
[英]Post multiple images using SLComposeViewController on Facebook/ Twitter?
我是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。 因此,僅發布一張圖片。
因此,我想知道如何實現自己的目標,並且是否可以在一條推文中發布多個圖像。
我認為您需要先創建一個專輯。
這是指向Facebook相冊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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.