Facebook publish_actions权限将废弃,寻求iOS照片分享替代方案
替代Facebook
publish_actions 权限的照片分享方案 嘿,你遇到的这个问题我之前也帮同行处理过——publish_actions权限确实会在8月1日被Facebook彻底移除,原来依赖这个权限的FBSDKShareAPI调用肯定会失效。现在官方推荐用Share Dialog或者Feed Dialog来替代,这两个方案都不需要申请额外的发布权限,用户只要登录Facebook账号就能使用,体验还更顺畅。
方案一:用Share Dialog(首推!)
Share Dialog会优先调用用户手机上的Facebook原生App,没有的话就弹出网页窗口,整个分享流程由Facebook把控,我们不用再操心权限的事儿,而且它支持直接传递本地图片,完美适配你的需求。
修改后的分享代码:
- (void)shareBoothOnFacebook { // 整理要分享的图片数组,这部分和你原来的逻辑一致 NSMutableArray *photos = [[NSMutableArray alloc] init]; for (int i = 0; i < [self.arrSlidshowImg count]; i++) { UIImage *image = [UIImage imageWithData:[_arrSlidshowImg objectAtIndex:i]]; FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init]; photo.image = image; photo.userGenerated = YES; [photos addObject:photo]; } FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init]; content.photos = [photos copy]; // 替换为Share Dialog实现分享 FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init]; shareDialog.fromViewController = self; shareDialog.shareContent = content; // 自动选择分享方式:优先用原生App,无App则用网页 shareDialog.mode = FBSDKShareDialogModeAutomatic; if ([shareDialog canShow]) { [shareDialog showWithCompletion:^(FBSDKShareDialogResult result, NSError *error) { if (error) { NSLog(@"分享出错啦: %@", error.localizedDescription); } else if (result == FBSDKShareDialogResultCancelled) { NSLog(@"用户取消了分享"); nonpressTimer = [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(timeout) userInfo:nil repeats:NO]; } else { NSLog(@"分享成功!"); } }]; } else { // 若无法使用Share Dialog,降级用Feed Dialog或提示用户安装Facebook App NSLog(@"没法弹出分享窗口,检查下有没有装Facebook App呀"); [self shareWithFeedDialog]; } }
登录代码简化:
再也不用请求publish_actions权限了,登录只需要基础的公开资料权限就行(甚至如果只是为了分享,连额外权限都不用,Share Dialog会自动处理登录流程):
-(IBAction)loginToFacebook:(id)sender { FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; login.loginBehavior = FBSDKLoginBehaviorWeb; // 仅请求基础公开资料权限,足够支撑分享需求 [login logInWithPermissions:@[@"public_profile"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if (error) { NSLog(@"登录失败"); } else if (result.isCancelled) { NSLog(@"用户取消登录"); nonpressTimer = [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(timeout) userInfo:nil repeats:NO]; } else { NSLog(@"登录成功,准备分享"); [self shareBoothOnFacebook]; } }]; }
方案二:Feed Dialog(降级备选)
如果用户没装Facebook App且网页版分享受限,Feed Dialog可以作为备选。但要注意:Feed Dialog不能直接传本地图片,得先把图片传到你的服务器拿到公开URL,或者用Graph API上传图片获取ID后再分享。
示例代码:
- (void)shareWithFeedDialog { NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"caption"] = @"这是我分享的照片"; // 自定义分享描述 params[@"link"] = @"https://your-app-domain.com"; // 你的应用链接(可选) // 若要传图片,这里填写图片的公开可访问URL // params[@"picture"] = @"https://your-server.com/photo.jpg"; FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] initWithParameters:params]; FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init]; dialog.fromViewController = self; dialog.shareContent = content; dialog.mode = FBSDKShareDialogModeFeedBrowser; [dialog showWithCompletion:^(FBSDKShareDialogResult result, NSError *error) { if (error) { NSLog(@"Feed分享失败: %@", error.localizedDescription); } else if (result == FBSDKShareDialogResultCancelled) { NSLog(@"用户取消了Feed分享"); } else { NSLog(@"Feed分享成功!"); } }]; }
关键提醒:
- 务必在Facebook开发者后台正确配置你的App的Bundle ID和URL Scheme,否则分享流程可能无法正常跳转。
- Share Dialog是最优解,因为它支持本地图片直接分享,无需额外做上传操作。
- 现在不需要再向Facebook申请任何发布权限,这两个Dialog都是官方允许的合规方式,无需走权限审核流程。
内容的提问来源于stack exchange,提问作者Marc Matteo
相关产品推荐
相关产品推荐

