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. Howeve
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.
This extension fix the problem, check it out! https://github.com/alexruperez/ARFacebookShareKitActivity
Launch FBSDKShareKit from UIActivityViewController instead of the default Facebook share sheet.
I tricked UIActivityViewController
to use the text by adding a preview image to the share array.
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
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.
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.
- (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];
}