iOS Facebook 分享

时间:2025-01-30 07:47:21

以分享链接为例 
调用原生app分享,如果没有原生app则调用web界面分享 

FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];

    = [NSURL URLWithString:ShareURL];

    = NSLocalizedString(@"Share_Message", nil);

    

    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];

    = content;

    = self;

    = self;

    = FBSDKShareDialogModeNative;

    [dialog show];

 

 

 

 

#pragma mark - FaceBook Share Delegate

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {

    NSString *postId = results[@"postId"];

    FBSDKShareDialog *dialog = (FBSDKShareDialog *)sharer;

    if ( == FBSDKShareDialogModeBrowser && (postId == nil || [postId isEqualToString:@""])) {

        // 如果使用webview分享的,但postId是空的,

        // 这种情况是用户点击了『完成』按钮,并没有真的分享

        [self showHUDWithModelText:NSLocalizedString(@"Share_canceled", nil)];

    } else {

        //分享成功

        [self showHUDWithModelText:NSLocalizedString(@"Share_successfully", nil)];

    }

    

}

 

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

    FBSDKShareDialog *dialog = (FBSDKShareDialog *)sharer;

    if (error == nil && == FBSDKShareDialogModeNative) {

        // 如果使用原生登录失败,但error为空,那是因为用户没有安装Facebook app

        // 重设dialog的mode,再次弹出对话框

        = FBSDKShareDialogModeBrowser;

        [dialog show];

    } else {

        [self showHUDWithModelText:NSLocalizedString(@"Share_Failed", nil)];

    }

}

 

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

    [self showHUDWithModelText:NSLocalizedString(@"Share_canceled", nil)];

}