Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController for Facebook not Showing Default Text

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

like image 579
C6Silver Avatar asked Apr 24 '15 17:04

C6Silver


4 Answers

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.

like image 59
DeepFriedTwinkie Avatar answered Nov 08 '22 13:11

DeepFriedTwinkie


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

}
like image 43
Kashif Avatar answered Nov 08 '22 11:11

Kashif


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.

like image 3
odm Avatar answered Nov 08 '22 12:11

odm


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
like image 1
Yedy Avatar answered Nov 08 '22 13:11

Yedy