I have UIActivityViewController
with 4 options: Message / Mail / TW / FB. I want to send different activities for each option!
For eg: In iMessage
sheet, I need to put : String
, NSURL
, and UIImage
. In Mail I need to place string in Subject field, then String in body, UIImage
and NSURL
also. In TW/FB
I want to place image like socials post did it, also some String and NSURL
.
Do you have any idea if this is possible in iOS8, with Swift ?
I searched a lot for some pieces of code, did not found the best for me.
Objective-C equivalent of hennes answer:
@interface MyStringItemSource : NSObject <UIActivityItemSource> { } @end @implementation MyStringItemSource -(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController { return @""; } -(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType { if (activityType == UIActivityTypeMessage) { return @"String for message"; } else if (activityType == UIActivityTypeMail) { return @"String for mail"; } return nil; } -(NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(UIActivityType)activityType { if (activityType == UIActivityTypeMessage) { return @"Subject for message"; } else if (activityType == UIActivityTypeMail) { return @"Subject for mail"; } return @""; } -(UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(UIActivityType)activityType suggestedSize:(CGSize)size { if (activityType == UIActivityTypeMessage) { return [UIImage imageNamed: @"thumbnail-for-message"]; } else if (activityType == UIActivityTypeMail) { return [UIImage imageNamed: @"thumbnail-for-mail"]; } return [UIImage imageNamed: @"thumbnail-for-default"]; } @end
You should take advantage of the UIActivityItemSource
protocol. The activityItems
parameter of the initializer of UIActivityViewController
accepts either an array of data objects or an array of objects that implement the UIActivityItemSource
protocol.
As an example consider an item source like the following.
class MyStringItemSource: NSObject, UIActivityItemSource { @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject { return "" } @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? { if activityType == UIActivityTypeMessage { return "String for message" } else if activityType == UIActivityTypeMail { return "String for mail" } else if activityType == UIActivityTypePostToTwitter { return "String for twitter" } else if activityType == UIActivityTypePostToFacebook { return "String for facebook" } return nil } func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String { if activityType == UIActivityTypeMessage { return "Subject for message" } else if activityType == UIActivityTypeMail { return "Subject for mail" } else if activityType == UIActivityTypePostToTwitter { return "Subject for twitter" } else if activityType == UIActivityTypePostToFacebook { return "Subject for facebook" } return "" } func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String!, suggestedSize size: CGSize) -> UIImage! { if activityType == UIActivityTypeMessage { return UIImage(named: "thumbnail-for-message") } else if activityType == UIActivityTypeMail { return UIImage(named: "thumbnail-for-mail") } else if activityType == UIActivityTypePostToTwitter { return UIImage(named: "thumbnail-for-twitter") } else if activityType == UIActivityTypePostToFacebook { return UIImage(named: "thumbnail-for-facebook") } return UIImage(named: "some-default-thumbnail") } }
The above item source returns different string data objects, subjects and thumbnail images based on the activity type. To use, you just need to pass it into the UIActivityViewController
initializer.
UIActivityViewController(activityItems: [MyStringItemSource()], applicationActivities: nil)
Similarly, you could define a custom MyUrlItemSource
class that returns different URLs based on the selected activity and pass it along in the initializer.
UIActivityViewController(activityItems: [MyStringItemSource(), MyUrlItemSource()], applicationActivities: nil)
All of this is outlined in the official documentation for UIActivityViewController
and UIActivityItemSource
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With