Facebook 的 UIActivityViewController 不显示默认文本

UIActivityViewController for Facebook not Showing Default Text

我正在使用 UIActivityViewController,它提供一些默认文本和 link。对于所有社交媒体(短信、电子邮件、推特),都会显示默认文本和 URL。但是,在显示 URL 图像时使用 FB,默认文本不显示(它只是空白)。下面是代码:

    NSString *shareStr = [NSString stringWithFormat:@""some text"];
    NSURL *website = [NSURL URLWithString:@"website"];
    NSArray *shareAray = @[shareStr,website];

    [self viewWillDisappear:YES];

    UIActivityViewController *activityController = [[UIActivityViewController alloc]initWithActivityItems:shareAray
                                                                                    applicationActivities:nil];

    if([activityController respondsToSelector:@selector(popoverPresentationController)] )
        activityController.popoverPresentationController.barButtonItem = self.shareButton;


    [self presentViewController:activityController
                       animated:YES completion:nil];

    [activityController setCompletionHandler:^(NSString *activityType, BOOL completed){
        if (!activityType || UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            [self viewWillAppear:YES];
        }
    }];

更新: 正如其他人在下面所说的那样,FB 不再允许预填充。这是另一个 link 视频,它给出了允许和不允许的示例:https://developers.facebook.com/docs/apps/review/prefill

Facebook 似乎不再希望开发人员使用文本预填充帖子。来自 https://developers.facebook.com/docs/sharing/ios#ios-integration:

Use of the iOS share sheet is subject to Facebook Platform Policy, including section 2.3 which states that apps may not pre-fill. In the context of the share sheet, this means apps may not pre-fill the share sheet's initialText field with content that wasn't entered by people earlier in their use of the app.

他们似乎更愿意开发人员使用 SDK:

While you can use the iOS native view controller API (share sheet) directly, there are several reasons to use the native iOS Share dialog provided by the Facebook SDK. Using the native Share dialog provides a quick way to test that people have signed into Facebook on their iOS 6+ devices. This API also uses the same style block as other parts of the Facebook SDK.

我通过向共享数组添加预览 图像 来欺骗 UIActivityViewController 使用文本。

我发现当您安装了 Facebook 应用程序时会发生这种情况。通过删除 Facebook 应用程序,UIActivityViewController 可以正确显示预填充文本,并且图像预览也有所不同。它在图像底部显示站点 url。

我用了一个技巧来在 iOS 中分享来自 UIActivityViewController 的文本和图像。首先,我需要确定用户是否从 UIActivityView 单击了 facebook 图标,然后打开带有文本和图像的 facebook 共享对话框。这是共享文本和图像的唯一方法。下面是我的代码。我希望这可以为开发人员节省时间。由于 Facebook 更改了那里的政策,因此很难共享预填充文本。

请在.h文件中添加UIActivityItemSource作为Delegate

当用户点击分享按钮显示 UIActivityViewController 时调用此方法。

NSString *textObject = [NSString stringWithFormat:@"%@, %@",shareString,timeDuration];
NSMutableArray *activityItems = [NSMutableArray arrayWithObjects:self, textObject, shareImage, nil];
activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];

并且当用户点击它的 facebook 图标时。

pragma 标记 - UIActivityItemSource 协议

- (id)activityViewController:(UIActivityViewController )activityViewController itemForActivityType:(NSString )activityType {
    
    if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
    
        [self dismissViewControllerAnimated:activityController completion:nil];
        [self performSelector:@selector(postToFacebook) withObject:self afterDelay:1.0];}
    return nil;
}

-(void)postToFacebook {
    
    [FBSDKSettings setAppID:@""];
    NSArray *imagesListingArray = [[item objectForKey:@"imgs"] valueForKey:@"img"];
    NSString * shareImage = [imagesListingArray objectAtIndex:0];
    
    FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
    content.contentTitle = @"F-Spot";
    content.contentDescription = [NSString stringWithFormat:@"%@  %@", [item valueForKey:@"event_name"],[item valueForKey:@"duration"]];
    content.imageURL = [NSURL URLWithString:shareImage];
    
    FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init];
    shareDialog.mode = FBSDKShareDialogModeNative;
    if(shareDialog.canShow) {
        shareDialog.mode = FBSDKShareDialogModeFeedBrowser;
    }
    shareDialog.shareContent = content;
    shareDialog.delegate = self;
    [shareDialog show];

}

此扩展程序解决了问题,请查看! https://github.com/alexruperez/ARFacebookShareKitActivity

从 UIActivityViewController 启动 FBSDKShareKit 而不是默认的 Facebook 共享 sheet。

基于@Kashifs 解决方案。这是 Swift 4.2、Xcode 10 和 FacebookSDK 4.4 中的一个版本。

  import FBSDKShareKit

extension YourViewController: UIActivityItemSource{

    //MARK: - ActionSheet

    func showShareSheet(){
        let shareStr = "Text you want to share"
        let activItems =  [self,shareStr, #imageLiteral(resourceName: "YourImageName")] as [Any]
        let shareSheet = UIActivityViewController(activityItems: activItems, applicationActivities: nil)
        self.shareSheet = shareSheet
        self.present(shareSheet, animated: true, completion: nil)
    }


    //MARK: - FacebookPost

    func postToFacebook() {
        let quote = "Text you want to share"
        if let URL = URL(string: "https://yourURL"){
            let content : LinkShareContent = LinkShareContent(url: URL, quote: quote)
            let dialog = ShareDialog(content: content)
            dialog.mode = .shareSheet
            dialog.presentingViewController = self
            do{
                try dialog.show()
            }catch{
                print("\(error)")
            }
        }
    }


    //MARK: - UIActivityItemSource Protocol

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any?{
        if activityType == UIActivity.ActivityType.postToFacebook{
            self.shareSheet?.dismiss(animated: true, completion: {
                self.postToFacebook()
            })
        }
        return nil
    }

    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return ""
    }

}//endReferral/Invite-Share