FBSDKSharingDelegate 回调不起作用,即使发布工作正常
Posted
技术标签:
【中文标题】FBSDKSharingDelegate 回调不起作用,即使发布工作正常【英文标题】:FBSDKSharingDelegate callback not working, even when post works fine 【发布时间】:2015-08-18 18:30:43 【问题描述】:FBSDKSharingDelegate 回调不起作用。我可以使用 ios SDK 正常发布到 facebook,但我想检测发布是否成功并采取额外措施通知用户。但是,回调对我不起作用。委托方法没有被调用,我不知道为什么。
使用 ios8,Parse 作为我的后端。在 Parse 中,用户链接到 FB。我正在使用IOS模拟器。
我尝试过的: 我已确保授予、保存和链接到 Parse 用户的发布权限。我进行了检查,检测到“publish_actions”正常。发布工作正常,因为我可以在 facebook 帐户上看到该帖子。只是回调不起作用。我检查了我的 fb 设置,它看起来不错。为了在最底部进行良好的衡量,我已经包含了我的应用程序委托中的相关代码。我已经用 XXXX 屏蔽了机密密钥。
代码:
1st:查看用户是否登录 Parse,如果没有,发送登录并链接到 Facebook 帐户。完成后,我请求“发布”权限并将该附加权限链接到 Parse 用户。我知道这在我重新编译时可以正常工作,它会记住“发布”权限并直接进入帖子。
@interface FacebookAPIPost () <FBSDKSharingDelegate>
@end
@implementation FacebookAPIPost
-(void)shareSegmentFacebookAPI //if statement below
//1) logged in?, if not send to sign up screen
//2) else if logged in, link account to facebook account, then send post
//3) else send to post b/c signed up and linked already.
PFUser *currentUser = [PFUser currentUser];
if(!currentUser)
[self pushToSignIn];
else if(![PFFacebookUtils isLinkedWithUser:currentUser])
[self linkUserToFacebook:currentUser];
NSLog(@"user account not linked to facebook");
else
[self shareSegmentWithFacebookComposer];
-(void)linkUserToFacebook:currentUser
[PFFacebookUtils linkUserInBackground:currentUser withPublishPermissions:@[@"publish_actions"] block:^(BOOL succeeded, NSError *error)
if(error)
NSLog(@"There was an issue linking your facebook account. Please try again.");
else
NSLog(@"facebook account is linked");
//Send the facebook status update
[self shareSegmentWithFacebookComposer];
];
-(void)shareSegmentWithFacebookComposer
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"])
[self publishFBPost]; //publish
else
NSLog(@"no publish permissions"); // no publish permissions so get them, then post
[PFFacebookUtils linkUserInBackground:[PFUser currentUser]
withPublishPermissions:@[ @"publish_actions"]
block:^(BOOL succeeded, NSError *error)
if (succeeded)
NSLog(@"User now has read and publish permissions!");
[self publishFBPost];
];
这里是发帖的地方:
-(void) publishFBPost
FBSDKShareLinkContent *content = [FBSDKShareLinkContent new];
content.contentURL = [NSURL URLWithString:[self.selectedSegment valueForKey:@"linkToContent"]];
content.contentTitle = [self.selectedProgram valueForKey:@"programTitle"];
content.contentDescription = [self.selectedSegment valueForKey:@"purposeSummary"];
PFFile *theImage = [self.selectedSegment valueForKey:@"segmentImage"];
NSString *urlString = theImage.url;
NSURL *url = [NSURL URLWithString:urlString];
content.imageURL = url;
FBSDKShareDialog *shareDialog = [FBSDKShareDialog new];
[shareDialog setMode:FBSDKShareDialogModeAutomatic];
// [FBSDKShareDialog showFromViewController:self.messageTableViewController withContent:content delegate:self];
[shareDialog setShareContent:content];
[shareDialog setDelegate:self];
[shareDialog setFromViewController:self.messageTableViewController];
[shareDialog show];
以下委托方法不起作用。意思是贴完后,我可以在FB账号上看到,但是这些委托方法都没有执行。
#pragma mark - delegate methods
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results
// if ([sharer isEqual:self.shareDialog])
NSLog(@"I'm going to go crazy if this doesn't work.%@",results);
// Your delegate code
//
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error
NSLog(@"sharing error:%@", error);
NSString *message = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?:
@"There was a problem sharing, please try again later.";
NSString *title = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops!";
[[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer
NSLog(@"share cancelled");
控制台输出: 发布后我收到的唯一消息是几秒钟后出现此消息:
plugin com.apple.share.Facebook.post invalidated
请帮忙!
脚注:appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Initialize Parse.
[Parse enableLocalDatastore];
[Parse setApplicationId:@"XXXX"
clientKey:@"XXX"];
[PFFacebookUtils initializeFacebookWithApplicationLaunchOptions:launchOptions];
//Initialize Facebook
[FBSDKAppEvents activateApp];
return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
//Method added for facebook integration
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
- (void)applicationDidBecomeActive:(UIApplication *)application
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[FBSDKAppEvents activateApp];
【问题讨论】:
【参考方案1】:我认为我回答这个问题为时已晚,但其他人也可能陷入困境,这就是分享我的知识的原因。
As per Facebook API documentation
sharer:didCompleteWithResults: Sent to the delegate when the share completes without error or cancellation.
The results from the sharer. This may be nil or empty.
这可能是因为只有在帖子成功共享时才会调用此委托方法。如果失败,则调用另一个委托方法sharer:didFailWithError:
get。我认为 Facebook API 不需要添加 result 参数就是这种情况。
所以根据我的经验,如果 sharer:didCompleteWithResults
每当调用它就意味着成功。
【讨论】:
我已经测试了很多案例,并且sharer:didCompleteWithResults
被调用并不意味着帖子已经发布成功。您可以打开共享对话框并按“完成”按钮或“关闭”按钮取消发布,仍然看到 sharer:didCompleteWithResults
方法已被调用。以上是关于FBSDKSharingDelegate 回调不起作用,即使发布工作正常的主要内容,如果未能解决你的问题,请参考以下文章