Facebook Sharing SDK : Sharing UIImage to facebook - swift

I have a button in my app when tapped it calls this function to share a UIImage to facebook, but it doesn't work. I followed facebook's documentation and I don't know why it doesn't work.
This is my code
func shareImage(image: UIImage) {
let photo = Photo(image: image!, userGenerated: true)
let content = PhotoShareContent(photos: [photo])
try! ShareDialog.show(from: self, content: content)
}
Can you please help me.
Thank you.

I've had success with this link sharing content with Facebook.
let vc = SLComposeViewController(forServiceType:SLServiceTypeFacebook)
vc.add(image)
vc.add(URL(string: "http://www.example.com/")) // optional
vc.setInitialText("Initial text here.")
self.present(vc, animated: true, completion: nil)

Related

DropBox Chooser and documentsPicker for Swift developers 3.0

While the Chooser implementations for iOS are present Here. It is however limited to Objective-C. Is it possible to create a chooser in swift manually?
(A dropBox chooser)
I am also unable to sufficiently call the documentspicker functions, where one can pull any document from any app the user may have installed.
Thank you in advance
⭐Solved
From your project's capabilites. First enable both the iCloud serivces and the key Sharing, now import MobileCoreServices in your class. Finally extended the following three classes inside your class.
UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Now implement the following functions..
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let myURL = url as URL
print("import result : /(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
How to call all of this? Add the following bit of code to your clickable function..
func clickFunction(){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
Click your button. The following menu will pop up ..
In the case of DropBox. Upon clicking on any item. You will be redirected to your app. And the Url will be printed.
Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.
kUTTypePDF
Also if you feel like customizing your own menu bar. Add the following code and customize your own function inside the handler
importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })
Enjoy it.

Share a video and text on Twitter, Instagram and other services using UIActivityViewController

I am trying to share a video and a text on Instagram, Facebook, Twitter and the native services like Mail, Messages, .... I can not figure out how to get both, Instagram and Twitter to show up in the sharing actionsheet:
If i pass in an array of text and a URL as activity items into the controller, just Instagram shows up, but not Twitter.
let url: NSURL = NSURL() // a url that directs to a video
let items: [AnyObject] = ["Check out this video", url]
let shareable = UIActivityViewController(activityItems: items, applicationActivities: nil)
controller.presentViewController(shareable,
animated: true,
completion: nil)
If i create a class that implements the UIActivityItemSource protocol instead and use that as activityItems, just Twitter shows up, but not Instagram:
class VideoActivityItemSource: NSObject, UIActivityItemSource {
private let videoUrl: NSURL
private let shareText = "View the full video here!"
init(url: NSURL) {
self.videoUrl = url
}
func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
switch activityType {
case UIActivityTypePostToFacebook:
return self.videoUrl
case UIActivityTypeMail:
return self.videoUrl
default:
return ["text": self.shareText, "url": self.videoUrl]
}
}
func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {
return "Hey check this new cool app!!!"
}
func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String?, suggestedSize size: CGSize) -> UIImage? {
return nil
}
}
and then replace the items by this:
items = [VideoActivityItemSource(url: url)]
I have no idea why in this case Twitter won't show up in the action sheet. Does somebody have an idea how to solve this?
I found the answer. The correct way to do this is to use the implementation of the UIActivityItemSource protocol. The reason for Instagram not showing up in the second solution where i am using the VideoActivityItemSource class is that i am returning an empty String in the activityViewControllerPlaceholderItem function.
Although Apple's documentation says that the type of the object returned in this function does not have to match the type that is used by the itemForActivityType function, it actually needs to be processable by the sharing service. In the case of Instagram it needs to be a video or an image, otherwise Instagram does not show up as a sharing option in the actionsheet.
So the solution is to return a UIImage in the activityViewControllerPlaceholderItem function instead of an empty String, then both Twitter and Instagram will show up as sharing options.
func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
// this needs to return some random image to make sure Twitter and Instagram show up in the sharing actionsheet
return UIImage(named: "someImage")!
}
Make sure you have the Instagram app on your phone.
`let activityVC = UIActivityViewController(activityItems: yourobjectArray, applicationActivities: nil)
activityVC.setValue("clipSnapshot", forKey: "subject")
if let activityPopOver = activityVC.popoverPresentationController {
activityPopOver.sourceView = self.view
activityPopOver.permittedArrowDirections = self.subviewView.isHidden ? .up : .left
}
self.present(activityVC, animated: true, completion: nil)
}`
When you see the sharing window and still don't see Instagram then goto the end of the list.
Click on "More" and check if instagram and twitter are included

UIActivityViewController - Facebook, works perfectly on simulator, but not on a device

When I run through the Xcode simulator, I can post both a picture and a url. One a device, however, I get an entirely different dialog.
I've used another example on here to illustrate the code
let myWebsite = NSURL(string:"http://www.google.com/")
let img: UIImage = imageView.image!
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [img, url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
self.presentViewController(activityViewController, animated: true, completion: nil)
On the simulator, the dialog comes up perfectly, and it posts both the picture and url to Facebook.
When I run the exact same example on my phone, a totally different dialog comes up, where I'm asked to add a line ("say something about this") and then it posts this headline, along with the google url
Interestingly enough, on the simulator, the url is posted as a url in Facebook, on the device, it's posted as a hyperlink.
Ultimately, I would like the the image and the hyperlink to appear on Facebook. However, if I was able to get the image and the url to post, I would be thrilled.
Has anyone had similar experiences?

Deep link not working for a case

Hey I am developing an app for the version iOS7 and above, where I have to share a content via mail or message. I use the following code
let textToShare = "Just click the following link to view "
let urlToShare = NSURL(string: "croding.com://crodid/crodname")
let objectsToShare = [textToShare, urlToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
I have added the croding.com in plist. Its working fine (open our app from mail exactly what I want) only if the app is installed already otherwise this thing is just a text instead of link. I need to show it as link even app is not installed.
croding.com://crodid/crodname

UIActivityViewController share only text

I want to share just simple text using UIActivityViewController
I am using swift, with xcode 6.3
The code is very simple, work great for photos, but not just text,
I don't want to include any web URL with the objectsToShare, just clean text
Here is the code:
var objectsToShare: ["some text to share"]!
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
//handler for completed task
activityVC.completionHandler = {(activityType, completed:Bool) in
if !completed {
println("cancelled")
return
}
reportCompleted() //report to server it's done.
}
//Excluded Activities Code
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeCopyToPasteboard ]
//activate the share view
self.presentViewController(activityVC, animated: true, completion: {
(complete) in
println(complete)
})
Now I know that it's all depends on the user have Facebook - not only on his phone, but also on his setting.
(I tried that directly from my phone 6, which has Facebook setting, and not from the simulator)
This code once worked fine, and still working good for images, and not text, but from some reason, it stop working for text sharing, Maybe the text itself has something to do with, maybe my code is not good, or maybe Facebook, change something.
Thank you for any thought about the subject.
To make thing more strange I found 2 more strange things.
The code of publish text is working on a simulator (as long as you add in the setting facebook)
If I add to the web url it also work (in device) if I remove the web url it is not working.
//code that work:
var myWebsite = NSURL(string: "http://www.someurl.com/")
let textToShare = "Swift is awesome! Check out this website about it!"
objectsToShare = [textToShare , myWebsite!]
//continue regular.
//code that doesn't work:
let textToShare = "Swift is awesome! Check out this website about it!"
objectsToShare = [textToShare ]
It seems that it is not a bug but facebook policy update:
"After discussing this with our team regarding stripping the pre-filling message area of text, this is actually BY DESIGN with the new share extension.
Pre-filling texts violates Platform Policy 2.3 ..... So in all enforcing policy 2.3 is a feature and not a bug. "
https://developers.facebook.com/bugs/949486035103197/
I read and the problem isn't iOS 8.3, is FacebookApp v29 and they haven't fix it on today update. If you uninstall your FacebookApp it works.
I can´t post images, but if you try this code with Facebook app installed and without it you will see the difference
func didPressShare(sender: AnyObject) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("This is without facebook on device")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
As they said: "Sorry for the delay everyone but we have this fix scheduled for v31. The reason for the delay is the cycle for getting updates approved for the App Store takes time. We appreciate your patience."
https://developers.facebook.com/bugs/949486035103197/
They say it will be fixed on next update.
I just tested on my example, using this code:
#IBAction func shareMoment_Action(sender: AnyObject) {
let firstActivityItem = "This is a simple text"
var activityVC = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)
activityVC.excludedActivityTypes = [
UIActivityTypePostToWeibo,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop
]
activityVC.completionWithItemsHandler = {(activityType: String!, completed: Bool, arrayOptions: [AnyObject]!, error: NSError!) in
println(activityType)
}
self.presentViewController(activityVC, animated: true, completion: nil)
}
And share is working fine for every option selected!
It is working well on my side. Please check your code again!
I have been facing the same problem but have not been able to fix it. This is what I found out so far:
Bug can be fixed when removing Facebook.app from device (but it's not a solution)
Bug seems to persist on devices with Facebook.app >v28.0 & iOS8.3
Facebook.app v28.0 & iOS8.3 still works
Facebook.app v29.0 & iOS8.2 still works
Sharing URL's seems to be affected too by this bug
It's hard to tell if Facebook actually has something to do with this bug, as they have a policy of entering bogus release notes for updates.
Since the sharing feature is not a main feature in my app, I will wait until the release of iOS8.4 and/or Facebook.app v30.0
After doing some research, I was able to find the following solution to the problem:
func shareTapped() {
let vc = UIActivityViewController(activityItems: [detailImageView.image!, "Check out these photos! http://www.photolib.noaa.gov/nssl"], applicationActivities: [])
presentViewController(vc, animated: true, completion: nil)
So in ActivityItems, you can pass an image then comma and then pass a string variable or open " and enter your text directly " in the enable above this worked with Facebook and Twitter in the UIActivityViewController.