FBSDKSharingDelegate 回调不起作用,即使 post 工作正常
FBSDKSharingDelegate callback not working, even when post works fine
FBSDKSharingDelegate 回调无效。 我可以 post 使用 ios SDK 正常访问 facebook,但我想检测是否 post 成功并采取其他操作通知用户。但是,回调对我不起作用。委托方法没有被调用,我不知道为什么。
使用 ios8,Parse 作为我的后端。在 Parse 中,用户 linked 到 FB。我正在使用 IOS 模拟器。
我尝试过的:
我已确保向 Parse 用户授予、保存和 link 发布权限。我已经 运行 检查并检测到 "publish_actions" 正常。 posting 工作正常,因为我可以在 facebook 帐户上看到 post。只是回调不起作用。我检查了我的 fb 设置,看起来不错。为了在最底部进行良好的衡量,我已经包含了来自我的应用程序委托的相关代码。我已经用 XXXX 屏蔽了机密密钥。
代码:
1st:查看用户是否登录到 Parse,如果没有,请发送登录并 link 到 facebook 帐户。完成后,我向 Parse 用户请求 "publish" 权限和 link 额外权限。我知道这在我重新编译时有效 b/c,它会记住 "publish" 权限并直接进入 post.
@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];
}
}];
这里是制作post的地方:
-(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];
}
下面的委托方法无效。意思是 post 完成后,我可以在 FB 帐户上看到它,但是 none 这些委托方法执行。
#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");
}
控制台输出:
posting 后我收到的唯一消息是几秒钟后出现此消息:
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];
}
我想我回答这个问题为时已晚,但其他人也可能陷入其中,这就是分享我的知识的原因。
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.
可能是因为此委托方法仅在 post 成功共享时才被调用。如果失败,另一个委托方法 sharer:didFailWithError:
将被调用。我认为 Facebook API 应该不需要添加 result 参数。
所以根据我的经验,如果 sharer:didCompleteWithResults
每当调用它就意味着成功。
FBSDKSharingDelegate 回调无效。 我可以 post 使用 ios SDK 正常访问 facebook,但我想检测是否 post 成功并采取其他操作通知用户。但是,回调对我不起作用。委托方法没有被调用,我不知道为什么。
使用 ios8,Parse 作为我的后端。在 Parse 中,用户 linked 到 FB。我正在使用 IOS 模拟器。
我尝试过的: 我已确保向 Parse 用户授予、保存和 link 发布权限。我已经 运行 检查并检测到 "publish_actions" 正常。 posting 工作正常,因为我可以在 facebook 帐户上看到 post。只是回调不起作用。我检查了我的 fb 设置,看起来不错。为了在最底部进行良好的衡量,我已经包含了来自我的应用程序委托的相关代码。我已经用 XXXX 屏蔽了机密密钥。
代码:
1st:查看用户是否登录到 Parse,如果没有,请发送登录并 link 到 facebook 帐户。完成后,我向 Parse 用户请求 "publish" 权限和 link 额外权限。我知道这在我重新编译时有效 b/c,它会记住 "publish" 权限并直接进入 post.
@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];
}
}];
这里是制作post的地方:
-(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];
}
下面的委托方法无效。意思是 post 完成后,我可以在 FB 帐户上看到它,但是 none 这些委托方法执行。
#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");
}
控制台输出: posting 后我收到的唯一消息是几秒钟后出现此消息:
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];
}
我想我回答这个问题为时已晚,但其他人也可能陷入其中,这就是分享我的知识的原因。
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.
可能是因为此委托方法仅在 post 成功共享时才被调用。如果失败,另一个委托方法 sharer:didFailWithError:
将被调用。我认为 Facebook API 应该不需要添加 result 参数。
所以根据我的经验,如果 sharer:didCompleteWithResults
每当调用它就意味着成功。