0

I'm developing an App and I want to have a Share button that allows users to pick an app from a list that they can share from, which then opens up that app or some sort of dialog that lets them tweet, post, text message, etc.

I've achieved this on my Android app because it is extremely simple there to create an ACTION_SEND intent, and filter based on certain mimetypes for what apps to share with.

I want something similar with iOS8 and Swift but I can't find anywhere how to achieve this.

I don't want to have buttons in my app for sharing with Twitter, or sharing with Facebook, or ... I just want to ping a sharing resource that gets me the apps that are candidates to share with and then have the user choose one, which then proceeds to the app with my String.

Update

So, I figured out a way to do it:

var sharingItems = [AnyObject]()

sharingItems.append(NSString(string: "Whatever you want to share!"))

let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)

self.presentViewController(activityViewController, animated: true, completion: nil)

The phone then figures out what you could ping, such as Twitter, Tumblr, Messenger, etc.

Granted, I was a bit surprised to find Facebook wasn't on the list. I also didn't try this route because I though the Share Sheets were deprecated from iOS 8 in favor of Application Extensions.

If there is a better way or another way this should be done in iOS 8, let me know please :)

David
  • 6,021
  • 8
  • 39
  • 85
  • So you want to find out which sharing is possible instead of a hardcoded list? – quantumpotato Jan 27 '15 at 02:54
  • When sharing on Android, a list pops up with candidate apps that the user chooses, which then opens up the app with the data you passed filled in (that other app handles this). I was wondering if in iOS 8, there is a similar utility. The share extension seems like an option, but I can't find anywhere that shows how to hook into that. – David Jan 27 '15 at 02:59
  • It can be done in Obj C I am not sure how in Swift though (not that this helps). The Social Media package has most of this already built in, you just code the buttons to define where they send the information to ie Twitter or Facebook. – App Dev Guy Jan 27 '15 at 03:17
  • @SASmith Yes, I don't want to have to code in separate buttons, just one that lets the phone take care of it for you. See parent for update. – David Jan 27 '15 at 03:29
  • So automatically post or just show the possible options of where to post to? – App Dev Guy Jan 27 '15 at 03:33
  • Show possible options of where to post. I posted code in the parent about how I'm doing it now, which works. I wonder if there is an iOS way to do it though. – David Jan 27 '15 at 03:34
  • It can show possible options, but you have to code which destinations you wish to use, such as Facebook, Twitter, etc. It's as simple as assigning a button title and the appropriate destination code for the button pressed but on the posts already asked, I am not sure whether Swift can do this yet. iOS coding is not really very automated, and Apple are pretty consistent in leaving it to the programmer to code what functions should be enabled and not just allow the user to decide. – App Dev Guy Jan 27 '15 at 03:51

1 Answers1

4

Just to give you a more comprehensive solution (for which Facebook is in fact an option), you can use a UIActivityViewController and exclude the irrelevant items in order to share to social media, for example to share to Facebook and Twitter you can exclude everything else like so:

let activityViewController = UIActivityViewController(activityItems:
  ["Whatever you want to share!"], applicationActivities: nil)
let excludeActivities = [
    UIActivityTypeMessage,
    UIActivityTypeMail,
    UIActivityTypePrint,
    UIActivityTypeCopyToPasteboard,
    UIActivityTypeAssignToContact,
    UIActivityTypeSaveToCameraRoll,
    UIActivityTypeAddToReadingList,
    UIActivityTypePostToFlickr,
    UIActivityTypePostToTencentWeibo,
    UIActivityTypeAirDrop]
activityViewController.excludedActivityTypes = excludeActivities;

presentViewController(activityViewController, animated: true,
  completion: nil)

Here's a full list of the built-in activity types.

Lyndsey Scott
  • 35,633
  • 9
  • 90
  • 123
  • Cool, that definitely filtered. For some reason the FB one still doesn't show up. Might just be a setting in the FB app – David Jan 27 '15 at 05:41
  • @David Just checking...The Facebook app is on your device, right? – Lyndsey Scott Jan 27 '15 at 05:46
  • Yeah, it is. The only thing I'm passing along is the NSString and the documentation states that Facebook should react to NSStrings. – David Jan 27 '15 at 05:48
  • 1
    Figured it out. I wasn't signed into FB through the settings (weird, because I was through the app). – David Jan 27 '15 at 05:52
  • Is it possible to give my own preferred order of the apps? Say I want FB and Twitter to be the first ones in the Share Sheet, then followed by all the rest? – David Jan 27 '15 at 05:54
  • @David No, you can only order custom activities: http://stackoverflow.com/a/19061259/2274694 – Lyndsey Scott Jan 27 '15 at 05:56