Is it possible to share UI Image with assigned link to FB? - ios

My app suggests user to share some results(reached inside the App) which collected to one UIImage (so this is not the image with known URL)
I want sharer also to share the link to some page(lets say "stackoverflow.com")
The problem is that if i add a link to controller, sharing controller does not show UIImage(showing page logo or a snippet), otherwise correctly shows UIImage without link.
I use SLComposeViewController, but this is not obligatory.
Due to this issue I can't use FBSDKShareButton (if it's needed). FBSDKSharingDialog does not accept UIImages
code:
//image : UIImage
let fbShareController: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
fbShareController.addImage(image)
fbShareController.addURL(NSURL(string: "www.stackoverflow.com"))
self.presentViewController(fbShareController, animated: true, completion: nil)
Is this new Facebook policy, or my issue?
Thank you for any help.

What you ask is not possible with SLComposeViewController.
The reason is when you add an image it is attached to the post as image. When you add an URL though, SLComposeViewController's private implementation ( or Facebook's backend services ..hard to say which one) takes over and it parses the URL and provides an appropriate image (if one is available as a result of parsing the URL contents) to complement the link.
The post then becomes a post with URL which doesn't show locally attached images.

Related

Choose destination when saving file

Is it possible to make user able to choose a destination for the file that he wants to download, something like DocumentPicker which you can use when choosing a file to upload?
I want something like this:
Yes, for iOS 13 and later you can ask the user to select a directory via UIDocumentPickerViewController. You'll get back a security scoped url(s) for the directories selected by the user.
Details here: https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories
I've pasted the sample code from that page below, but you'll want to read the documentation carefully because security scoped URLs require careful handling :)
If you need iOS 12 or earlier the user can only select files so I'm unclear on a clean way to do this (but we're on iOS 14 and iOS 15 is about to come out so hopefully you don't have to support back past iOS 13).
Here's the sample code from the link above showing how this is done:
// Create a document picker for directories.
let documentPicker =
UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
documentPicker.delegate = self
// Set the initial directory.
documentPicker.directoryURL = startingDirectory
// Present the document picker.
present(documentPicker, animated: true, completion: nil)

UIActivityController does not report the text and image when sharing on Facebook

I have this simple piece of code to display a sharing dialog for a number of social avenues:
let text = String(format:NSLocalizedString("I %# with the Dhammapada verse \"%#\"", comment:"$ to be composed with $$"), part, verse)
let url = URL(string:"http://www.ipuja.net")
let image = buddhanetImage.image ?? UIImage(named:"Icon-72")
let shareController=UIActivityViewController(activityItems:[text, url!, image!], applicationActivities:nil)
shareController.excludedActivityTypes = [UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList,UIActivityTypePostToVimeo];
if (UI_USER_INTERFACE_IDIOM() == .pad){
shareController.popoverPresentationController?.sourceView = self.buddhanetImage;
}
self.present(shareController, animated:true, completion:nil)
Yet the tool woks fine for Google and Twitter, but when I try to share the contents on Facebook, just the url is shown without either text of image.
I checked that doing the thing on Photos and other other third party apps allows to display the full content even for Facebook, so what is missing in my code for that to work?
The URL will override the text and image when sharing to Facebook from a third-party application. Remove the URL and the image and text will appear. Also, if you have multiple hashtags in your text, only the first hashtag will be applied.
Prior to iOS 9, prefilling the Facebook share dialog with text, image, and URL was possible. Once iOS 9 was released, prefilling any fields when sharing to Facebook was not possible at all. Sometime around iOS 9.3 they reenabled prefilling fields, but prefilling all the fields is not possible.

How to get the default iOS browser name?

The Stack Overflow app detects the name of my jailbrokenly-set default browser (Chrome). How can I achieve the same thing in objective-c and swift?
(Just the name, not the ActivityView code)
Example:
Update: I went into Settings > Stack Exchange and found this:
It looks like the app defaults to Safari, but iff Chrome is installed then links will be sent to that browser. Chrome is most likely detected by the canOpenUrl method described in the answer below.
I suspect that the Stack Exchange app isn't checking for your default browser specifically. After all, since Apple doesn't provide an easy way to change your default browser, I doubt the SDK provides an API to detect the default browser.
Instead, the Stack Exchange app may use the canOpenURL(_:) method on UIApplication to test if a predetermined set of common browser apps are installed. For each browser that is installed, the action sheet gets a new button. That approach could resemble the following code snippet.
let safariURL = NSURL(string: "http://stackoverflow.com")!
let chromeURL = NSURL(string: "googlechrome://stackoverflow.com")!
let operaURL = NSURL(string: "opera-http://stackoverflow.com")!
let sharedApplication = UIApplication.sharedApplication() // convenience
if sharedApplication.canOpenURL(safariURL) {
// add "Safari" button to action sheet
}
if sharedApplication.canOpenURL(chromeURL) {
// add "Chrome" button to action sheet
}
if sharedApplication.canOpenURL(operaURL) {
// add "Opera" button to action sheet
}
// display action sheet

NSData returns nil when contentsOfUrl constructor is called

let myProfilePictureURL = NSURL(string: "http://graph.facebook.com/bobdylan/picture")
let imageData = NSData(contentsOfURL: myProfilePictureURL!)
In the above code, imageData is nil after execution. Any idea, what could be wrong here?
The issue is actually you don't have any data in that specified url. If you paste that on the browser, it redirects to some other url and loads the image/data. That's why you are getting nil there.
Facebook always provides a absolute URL to your profile image. You can paste that url in any browser and will display that image. Please check the image url that you are getting from Facebook.
You're not getting data because you're not trying to laod the image but the "page". Using your link, I can see the image, and considering what's in the url i suggest you replace
let myProfilePictureURL = NSURL(string:"http://graph.facebook.com/bobdylan/picture")
with this
let myProfilePictureURL = NSURL(string:"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/v/t1.0-1/p50x50/63781_10154869387235696_5558117780840742364_n.jpg?oh=92218e0caca2cbaa28266ef30757d4e2&oe=55780E74&__gda__=1434574503_cdb40e50a7c5770845fbbb6098bf073a")
I got this URL by either copying what's in the browser navigation bar (on the top of the screen) or simply by right cliking and then clicking "Copy URL of Image".

How do I create a "pin it" URLs for videos (for Pinterest)?

I'm creating dynamic WordPress "pin it" buttons and have it working on pictures.
For example, when I generate this URL it launches pinterest's pin it page:
http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.mywebsite.com%2Ftest-post%2F&media=http://www.mywebsite.com/wp-content/uploads/2012/09/picture.jpg&description=Test+Post
How do I go about doing this for a Vimeo or Youtube video?
http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.mywebsite.com%2Ftest-post%2F&media=________????_________&description=Test+Post
Do I just put a vimeo or youtube URL there?
To pin a Youtube video you just need to media link to be a youtube thumbnail. Pinterest then automatically recognizes it as a video.
To get the youtube thumbnail you need to get the video id from the youtube url as explained more here
var youtubelink = "http://www.youtube.com/embed/wkBstmXvGtk"
var youtubecode = youtubelink.match(/\/embed\/(.*)/)[1];
var pinimage = 'http://img.youtube.com/vi/' + youtubecode + '/0.jpg';
I wrote a full tutorial on it here:
http://thingsilearned.com/things/pin-button
Unfortunately, I think the script used to generate a web-based pin is not capable of handling video.
I utilized the following code...
<img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" />
<script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>
...where the URL was the link to the YouTube video (or permalink). The problem is, the pinit.js script does not parse image information from the url or provide a video screenshot for the pin. It comes up blank, and without a value for media, the script won't create the pin.
To confirm the problem is within pinit.js, I substituted that script for pinmarklet.js. What that script does is create an overlay on the page, with the screen shot of a video and a "PIN IT" button overlay on the screen shot. But that's probably not the type of user experience you're looking to present.
One possible option (and you could get away with this through feeding the URL, media and description values dynamically) is to feed the URL with the video URL, create a screenshot and feed that into media, and finally, add the description. The drawback to this is, you'll only be pinning the image and not the video directly.
Sorry to be the bearer of bad news.

Resources