tbago tbago - 1 month ago 12
iOS Question

FBSDKShareDialog delegate not called

I use the latest Facebook SDK for iOS platform.I use FBSDKShareDialog to share image to Facebook,the code can share image to Facebook.But I want to get the share result delegate.

- (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 {

}


Also I add suggest code in 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];
}


But the FBSDKSharingDelegate method has been never called.

Answer

You are setting fromViewController properly but that controller should be the delegate not the object.Below is code that is working and delegate is getting called

- (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");
}
Comments