问题
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 :)
回答1:
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.
来源:https://stackoverflow.com/questions/28162455/how-to-share-a-string-with-sharing-apps-like-facebook-twitter-etc