没有调用FBSDKShareDialog委托

tba*_*ago 2 delegates facebook objective-c ios

我使用最新的Facebook SDK for iOS平台.我使用FBSDKShareDialog将图像分享到Facebook,代码可以共享图像到Facebook.但我想获得共享结果委托.

- (void)sharedImage:(UIImage *) sharedImage
 fromViewController:(UIViewController *) fromViewController {
    FBSDKSharePhoto *photo  = [[FBSDKSharePhoto alloc] init];
    photo.image             = sharedImage;
    photo.userGenerated     = YES;

    FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
    content.photos = @[photo];

    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
    dialog.fromViewController = fromViewController;
    dialog.shareContent = content;
    dialog.delegate = self;
    dialog.mode = FBSDKShareDialogModeShareSheet;
    [dialog show];
}
#pragma mark - FBSDKSharingDelegate
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {

}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {

}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {

}
Run Code Online (Sandbox Code Playgroud)

我还在AppDelegate.m中添加了建议代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];
    return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSDKAppEvents activateApp];
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    [[FBSDKApplicationDelegate sharedInstance] application:application
                                                   openURL:url
                                         sourceApplication:sourceApplication
                                                annotation:annotation];
}
Run Code Online (Sandbox Code Playgroud)

但是从未调用过FBSDKSharingDelegate方法.

Muk*_*esh 6

你正在设置fromViewController正确,但该控制器应该是委托而不是object.Below是正在运行的代码和委托被调用

- (IBAction)facebookButtonClick:(UIButton *)sender {
    //    FacebookShare *facebookShare = [[FacebookShare alloc] init];
    //    [facebookShare sharedImage:[UIImage imageNamed:@"facebook_share"]
    //            fromViewController:self];
    FBSDKSharePhoto *photo  = [[FBSDKSharePhoto alloc] init];
    photo.image             = [UIImage imageNamed:@"facebook_share"];
    photo.userGenerated     = YES;

    FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
    content.photos = @[photo];

    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
    dialog.fromViewController = self;
    dialog.delegate = self;
    dialog.shareContent = content;
    dialog.mode = FBSDKShareDialogModeShareSheet;
    [dialog show];
}

//FBSDKSharingDelegate
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
    NSLog(@"completed");
}

- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
    NSLog(@"fail %@",error.description);
}

- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {
    NSLog(@"cancel");
}
Run Code Online (Sandbox Code Playgroud)