45

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:

    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

C6Silver
  • 3,127
  • 2
  • 21
  • 49
  • I also found this issue with iOS 8.2. – SanitLee May 01 '15 at 01:54
  • I'm seeing this as well on iOS 8.3, do you have a issue number @DanielStorm ? – francis May 01 '15 at 18:22
  • Just to confirm FB app plays a big part on this issue. I don't see the issue with the previous FB app installed but the latest. So as @DanielStorm said perhaps wait for new updated FB app. – SanitLee May 03 '15 at 05:10
  • 1
    It is the same policy that have been using android years. Not prefill text, if you want share with facebook put a facebook share button, not default share – encastellano Jun 10 '15 at 13:52
  • 1
    @Daniel Storm It's not a bug - it's a feature of latest Facebook app versions. From some time, Facebook policy doesn't allow text pre-filling. It will not be "fixed" so there is no point in waiting. – mkll Jul 31 '15 at 12:41

6 Answers6

50

It would seem Facebook no longer wants developers to pre-fill posts with text. From 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.

Seems they would prefer devs to use the 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.

DeepFriedTwinkie
  • 3,865
  • 1
  • 21
  • 21
5

I have done one trick to share text and image from UIActivityViewController in iOS. First I need to find if user have clicked on facebook icon from UIActivityView and 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.

Please add UIActivityItemSource in .h file as Delegate

This will be called when user click on share button to show 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.

pragma mark - UIActivityItemSource Protocol

- (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];

}
Community
  • 1
  • 1
Kashif Jilani
  • 1,207
  • 3
  • 22
  • 49
  • can you illustrate please how postToFacebook function should work , as it's full with errors , how to make it work please ... :(( – Mostafa Sultan Feb 25 '16 at 12:05
  • 'contentTitle' is deprecated: `contentTitle` is deprecated from Graph API 2.9 `contentDescription` is deprecated from Graph API 2.9 – Ana Paula Sep 07 '17 at 19:59
3

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

odm
  • 888
  • 1
  • 14
  • 22
  • 2
    In iOS9, the Facebook activity will not show as an option, nor will it appear in the "..." list once you remove the Facebook app from the device. – E.Freitas Sep 21 '15 at 18:31
  • 1
    I did not find @E.Freitas 's comment to be true. If you are logged-in to Facebook via the iPhone's Settings App and do not have the Facebook App installed, there is still a Facebook share option in the UIActivityViewController – Chris Jan 25 '16 at 00:26
1

Based on @Kashifs solution. Here is a version in Swift 4.2, Xcode 10 and 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
Yedy
  • 2,107
  • 1
  • 25
  • 30
0

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

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

alexruperez
  • 103
  • 1
  • 8
-1

I tricked UIActivityViewController to use the text by adding a preview image to the share array.

r-dent
  • 685
  • 8
  • 22