Reputation: 2279
I'm trying to allow the user to post a simple Facebook status update, without using OpenGraph. So far, allowing the user to log in and asking for publish_actions
permissions, goes well without a hitch.
However, when I try to call presentShareDialogWithLink:name:caption:description:picture:clientState:handler:
it always returns nil and shows nothing. It doesn't even appear to call the handler, which leaves at a loss as to why it doesn't work.
What are the reasons that this can fail? If I knew what might be the reason, I can always retrace my steps.
Relevant code
User presses the button
AppDelegate_Pad *appDelegate = (AppDelegate_Pad *)[[UIApplication sharedApplication] delegate];
if(FBSession.activeSession.isOpen) {
[appDelegate postFacebookUpdateWithAlbum:album];
} else {
// The person using the app has initiated a login, so call the openSession method
// and show the login UX if necessary.
[appDelegate openSessionWithAllowLoginUI:YES album:album];
}
Facebook code in App Delegate
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([FBSession.activeSession handleOpenURL:url]) {
[self postFacebookUpdateWithAlbum:self.facebookAlbum];
self.facebookAlbum = nil;
return YES;
}
}
#pragma mark Facebook
/*
* Opens a Facebook session and optionally shows the login UX.
*/
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI album:(LPAlbum *)album {
self.facebookAlbum = album;
return [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session
state:status
error:error];
}];
}
- (void)postFacebookUpdateWithAlbum:(LPAlbum *)album {
if (album) {
// we defer request for permission to post to the moment of post, then we check for the permission
if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
// if we don't already have the permission, then we request it now
[FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
[self doPostWithAlbum:album];
}
else {
DLog(@"Could not get permissions. Error: %@", error);
}
}];
} else {
[self doPostWithAlbum:album];
}
}
}
- (void)doPostWithAlbum:(LPAlbum *)album {
NSString *initialText = [NSString stringWithFormat:@"Neat status update"];
NSURL *coverImageURL = [NSURL URLWithString:album.imageBaseURLString];
UIImage *coverImage = [UIImage imageWithContentsOfFile:album.imageBaseURLString];
NSURL *infoLinkURL = [NSURL URLWithString:album.infoSiteURLString];
// First, try the iOS 6 native sharing.
BOOL iOS6native = [FBDialogs presentOSIntegratedShareDialogModallyFrom:self.window.rootViewController initialText:initialText image:coverImage url:infoLinkURL handler:^(FBOSIntegratedShareDialogResult result, NSError *error) {
}];
if (!iOS6native) {
NSString *name = @"name";
NSString *caption = @"caption";
NSString *description = @"description";
FBShareDialogParams *params = [[FBShareDialogParams alloc] init];
params.link = infoLinkURL;
params.name = name;
params.description = description;
BOOL canPresent = [FBDialogs canPresentShareDialogWithParams:params]; // returns NO
DLog(@"Can present with params? %@", canPresent ? @"Yes" : @"No");
canPresent = [FBDialogs canPresentOSIntegratedShareDialogWithSession:[FBSession activeSession]]; // returns NO
DLog(@"Can present with session? %@", canPresent ? @"Yes" : @"No");
FBAppCall *appCall = [FBDialogs presentShareDialogWithLink:infoLinkURL
name:name
caption:caption
description:description
picture:coverImageURL
clientState:nil
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if (error) {
DLog(@"Error: %@", error.description);
} else {
DLog(@"Success!");
}
}];
if (!appCall) {
// App call failed.
}
}
}
// Convenience method to perform some action that requires the "publish_actions" permissions.
- (void) performPublishAction:(void (^)(void)) action {
// we defer request for permission to post to the moment of post, then we check for the permission
if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
// if we don't already have the permission, then we request it now
[FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
action();
}
//For this example, ignore errors (such as if user cancels).
}];
} else {
action();
}
}
Upvotes: 2
Views: 2491
Reputation: 15662
presentShareDialogWithLink requires the Facebook app (it's documented here, and says "Presents a dialog in the Facebook application ...").
If you want to use the iOS6 system dialog, you want one of the presentOSIntegratedShareDialogModallyFrom methods, like this one.
Upvotes: 2