objective-c Facebook 的 UIActivityViewController 不显示默认文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29854282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:34:01  来源:igfitidea点击:

UIActivityViewController for Facebook not Showing Default Text

objective-cxcodefacebookuiactivityviewcontroller

提问by C6Silver

I am using a UIActivityViewController which provides some default text and a link. With all social mediums (sms, email, twitter) the default text and URL are shown. However, with FB while the URL image is shown, the default text is not showing (it is just blank). Below is the code:

我正在使用提供一些默认文本和链接的 UIActivityViewController。对于所有社交媒体(短信、电子邮件、推特),都会显示默认文本和 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];
        }
    }];

Update: As noted in what others said below, FB no longer allows pre-fill. Here is another link to a video that gives examples of what is allowed and what isn't: https://developers.facebook.com/docs/apps/review/prefill

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

回答by DeepFriedTwinkie

It would seem Facebook no longer wants developers to pre-fill posts with text. From https://developers.facebook.com/docs/sharing/ios#ios-integration:

看起来 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.

iOS 共享表的使用受 Facebook 平台政策的约束,包括第 2.3 节,其中规定应用程序不得预填充。在共享表的上下文中,这意味着应用程序可能不会使用人们在使用应用程序之前未输入的内容预先填充共享表的 initialText 字段。

Seems they would prefer devs to use the SDK:

似乎他们更喜欢开发人员使用 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.

虽然您可以直接使用 iOS 本机视图控制器 API(共享表),但使用 Facebook SDK 提供的本机 iOS 共享对话框有几个原因。使用本机“分享”对话框提供了一种快速测试人们是否在 iOS 6+ 设备上登录 Facebook 的方法。此 API 还使用与 Facebook SDK 其他部分相同的样式块。

回答by Kashif

I have done one trick to share text and image from UIActivityViewControllerin iOS. First I need to find if user have clicked on facebook icon from UIActivityViewand then open the share dialog of facebook with text and image. This is the only way to share for both text and image. Below is my code. I hope this can save time for the developers. Since Facebook have changed there policies and its really hard to share pre fill text.

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

Please add UIActivityItemSourcein .h file as Delegate

UIActivityItemSource在 .h 文件中添加为 Delegate

This will be called when user click on share button to show UIActivityViewController.

当用户单击共享按钮以显示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];

And When user click on facebook icon from it.

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

pragma mark - UIActivityItemSource Protocol

pragma mark - 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];

}

回答by odm

I've found it happens when you have the Facebook app installed. By removing the Facebook app the UIActivityViewControllershows the pre-filled text correctly and the image preview is also different. It shows the site url at the bottom of the image.

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

回答by alexruperez

This extension fix the problem, check it out! https://github.com/alexruperez/ARFacebookShareKitActivity

这个扩展解决了这个问题,看看吧!https://github.com/alexruperez/ARFacebookShareKitActivity

Launch FBSDKShareKit from UIActivityViewController instead of the default Facebook share sheet.

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

回答by Yedy

Based on @Kashifs solution. Here is a version in Swift 4.2, Xcode 10 and FacebookSDK 4.4.

基于@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

回答by r-dent

I tricked UIActivityViewControllerto use the text by adding a preview imageto the share array.

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