I am trying to share an image with description to the facebook from my app using FBSDKShareKit. but the problem is, I am unable to add description with the image .
Currently I have:
let content: FBSDKSharePhotoContent = FBSDKSharePhotoContent()
let photo:FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = imageView.image
photo.isUserGenerated = true
content.photos = [photo]
content.contentURL = URL(string:directUrl)
FBSDKShareDialog.show(from: self, with: content, delegate: nil)
I also have tried:
let vc = SLComposeViewController(forServiceType:SLServiceTypeFacebook)
vc?.add(imageview.image)
vc?.add(URL(string: "http://www.example.com/"))
vc?.setInitialText("description")
self.present(vc!, animated: true, completion: nil)
using social ,In which setInitialText does not work according to this answer
I tried this too:
Is there any other way in which I can share image with description to fb?
Related
I am trying to share an image + text via WhatsApp.
Here is my code:
let activityViewController = UIActivityViewController(activityItems: [imageNmae, "Share text"], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop]
activityViewController.completionWithItemsHandler = {(activityType: UIActivity.ActivityType?, completed: Bool, returnedItems:[Any]?, error: Error?) in
self.shareMainView.isHidden = true
}
if let viewController = UIApplication.topMostViewController {
viewController.present(activityViewController, animated: true) {
// self.shareMainView.isHidden = true
}
}
How can I achieve this?
I can see some other apps sharing a URL and it's showing both an image and text. Even Android is able to share. But if execute the above code, I can only share text and not an image.
Share URL of the image along with your text.
WhatsApp will create preview from the link, and make it appear like image + text.
There is no real way of sending image + text on WhatsApp (in Nov 2021)
I have tried different ways to show a URL icon with text but didnt succeed. Here is the code and its output snippets
Example 1:
let text = "I am text"
let myWebsite = URL(string:"https://www.youtube.com/")
let shareAll = [text, myWebsite] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Output 1:
UIActivity View Messages View
Example 2:
let myWebsite = URL(string:"https://www.youtube.com/")
let shareAll = [myWebsite] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Output 2:
UIActivity View
Required:
I want URL icon like in Output 2 for Example 1.
There is a difference between sharing data and showing data - I think in your case, showing the data that's about to be shared.
I could be wrong, but if not, you are actually able to share the data - in this case a UIImage and text - but wish to display what is about to be shared. I had this issue a while back. Here's my code. See if it can help you - basically you need to add metadata to UIActivityViewController delegate.
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let shareImage = preview.uiImage.adjustedForShareSheetPreviewIconProvider()
let imageProvider = NSItemProvider(object: shareImage)
let metadata = LPLinkMetadata()
metadata.imageProvider = imageProvider
metadata.title = "Title"
return metadata
}
Adjust shareImage and title as needed.
EDIT
I was using my code and was incomplete. adjustedForShareSheetPreviewIconProvider() is part of UIImage, and as such, it assumes this. In my code, I'm actually calling a GLKView to return this UIImage and then use it this way in the metadata.
So if your image is something returned from the URL as an icon and is (or can be converted to) a UIImage, just add it to the metadata like my code above.
I want to share an HTML link on facebook without install SDK like WhatsApp or twitter. I am using below code for share a link on facebook. But I want to share the post to direct app without an open safari browser.
Please tell me this is possible or not.I have searched alot but not find any solution.
//For share twitter
let urlWhats = "twitter://post?message=\(fileURL)"
let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let whatsappURL = URL(string: urlString!)
if UIApplication.shared.canOpenURL(whatsappURL!)
{
UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil)
}
//For Facebook
let shareURL = "https://www.facebook.com/sharer/sharer.php?u=" + url
if let url = URL(string:shareURL)
{
let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
vc.delegate=self
present(vc, animated: true)
}
Thanks in Advance
For sharing in FB make use of 'FacebookShare'
import FBSDKShareKit
class yourViewController: UIViewController {
func shareContentInFB() {
let content = FBSDKShareLinkContent()
content.contentURL = URL(string: "http://yourURL/")
content.quote = "Hey !!!!"
let dialog : FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.shareContent = content
dialog.mode = FBSDKShareDialogMode.automatic
dialog.show()
}
}
You cannot prefill text in fb using this, if you have to do it then make use of Fb graph API
I created a code swift, that use facebook SDK 4.x. The code use FBSDKShareButton and worked very well.
This is the code:
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "http://alexandreccarmo.com")
content.contentTitle = "Teste"
content.contentDescription = "teste teste"
content.imageURL = NSURL(string: "xxxx/2.jpg")
let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25)
self.view.addSubview(button)
But I need to share without use facebook dialog. When the user click on button of my app I need share directly on their facebook.
I tried this:
let contentx : FBSDKShareLinkContent = FBSDKShareLinkContent()
contentx.contentURL = NSURL(string: "http://alexandreccarmo.com")
contentx.contentTitle = "Testexxxxx"
contentx.contentDescription = "teste testexxx"
contentx.imageURL = NSURL(string: "xxxx/2.jpg")
FBSDKShareAPI.shareWithContent(contentx, delegate: nil)
But I receive this error:
FacebookLogin[6721:3464359] FBSDKLog: Warning: [FBSDKAccessToken currentAccessToken] is missing publish_actions permissions
I read on facebook developers that I need to request "publish_actions" when I need to send share. But I don't understand how can i do this. Somebody knows?
So here is some code that I used to publish an image on Facebook without the Facebook SDK:
func actionTapped(sender: AnyObject) {
let objectsToShare = //reference to image
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.completionWithItemsHandler = actionCompleted
presentViewController(activityVC, animated: true, completion: nil)
}
func actionCompleted(activityType: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) {
// Return if cancelled
if (!completed) {
return
}
close()
}
You can check out these links as well:
Can I use UIActivityViewController to share on Facebook ?
http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/
UIActivityViewController share only text
I hope this helps.
I'm trying to post a photo to Facebook, and it works fine using the following code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
self.selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
var photo = FBSDKSharePhoto()
photo.image = self.selectedImage
photo.userGenerated = true
var content = FBSDKSharePhotoContent()
content.photos = [photo]
self.dismissViewControllerAnimated(true, completion: nil)
shareDialog.fromViewController = self
shareDialog.shareContent = content
shareDialog.show()
}
However, this only works when the user has the facebook app downloaded. otherwise it does NOT open the web browser to share the photo, even though facebook state:
The Share Dialog switches to the native Facebook for iOS app, then
returns control to your app after a post is published. If someone
doesn't have Facebook app installed it will automatically falls back
to a web-based dialog.
Any suggestions please ? answers appreciated, thank you.
You can try this:
func postToFacebook(image: UIImage!) {
var SocialMedia :SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
SocialMedia.completionHandler = {
result -> Void in
var getResult = result as SLComposeViewControllerResult;
switch(getResult.rawValue) {
case SLComposeViewControllerResult.Cancelled.rawValue: println("Cancelled")
case SLComposeViewControllerResult.Done.rawValue: println("It's Work!")
default: println("Error!")
}
self.dismissViewControllerAnimated(true, completion: nil)
}
self.presentViewController(SocialMedia, animated: true, completion: nil)
SocialMedia.setInitialText("...")
SocialMedia.addImage(image)
}