Share Image with Hashtag via Uiactivityviewcontroller (Twitter, Facebook, Instagram)

Share image with hashtag via UIActivityViewController (Twitter, Facebook, Instagram)

Define two UIActivityItemSource classes, one for Image and one for Text.

In first one only return the image.
In second one return NSObject() for placeHolder, and return Text or nil depending on activity. By returning NSObject(), UIActivity will allow all services to be available.

UIActivityViewController(activityItems: [ImageProvider(), TextProvider()], applicationActivities: nil)

and providers:

class TextProvider: NSObject, UIActivityItemSource {
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return NSObject()
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
if activityType == .postToTwitter || activityType == .postToFacebook {
return "Tweet with #Hashtag"
}
return nil
}
}

class ImageProvider: NSObject, UIActivityItemSource {
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return UIImage(named: ...)
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
return UIImage(named: ...)
}
}

Explaination

First of all, Keys are not really sensitive, The only sensitive-key was "subject" for email and apps supporting it, which is implemented in UIActivityController's API and we can set it directly. It doesn't matter if you provide UIImage with key "image" or "1".

As it turns out, Twitter activity will not work if it's text is not returned directly in ...itemForActivity... method. So the solution is to separate item sources.

Twitter activity also will not work, if placeholder receives anything other than String, but by returning String Instagram activity will not work, So by returning NSObject() Type will be ignored and all services will be available.
if you want to limit some services use UIActivityViewController.excludedActivityTypes

UIActivityViewController for Facebook, Twitter and Instagram ONLY

There are option available for Facebook and Twitter, You need to just add excludedActivityTypes. You have missing it.

Instagram option still not available in activity types

let image = UIImage(named: "TheImage")
let activityViewController = UIActivityViewController(activityItems: [shareText,image], applicationActivities: nil)

activityViewController.excludedActivityTypes = [.addToReadingList,
.airDrop,
.assignToContact,
.copyToPasteboard,
.mail,
.message,
.openInIBooks,
.print,
.saveToCameraRoll,
.postToWeibo,
.copyToPasteboard,
.saveToCameraRoll,
.postToFlickr,
.postToVimeo,
.postToTencentWeibo,
.markupAsPDF
]

present(activityViewController, animated: true, completion: {})

I hope this will help you.

There are following list of Activity types available.

extension UIActivityType {

@available(iOS 6.0, *)
public static let postToFacebook: UIActivityType

@available(iOS 6.0, *)
public static let postToTwitter: UIActivityType

@available(iOS 6.0, *)
public static let postToWeibo: UIActivityType // SinaWeibo

@available(iOS 6.0, *)
public static let message: UIActivityType

@available(iOS 6.0, *)
public static let mail: UIActivityType

@available(iOS 6.0, *)
public static let print: UIActivityType

@available(iOS 6.0, *)
public static let copyToPasteboard: UIActivityType

@available(iOS 6.0, *)
public static let assignToContact: UIActivityType

@available(iOS 6.0, *)
public static let saveToCameraRoll: UIActivityType

@available(iOS 7.0, *)
public static let addToReadingList: UIActivityType

@available(iOS 7.0, *)
public static let postToFlickr: UIActivityType

@available(iOS 7.0, *)
public static let postToVimeo: UIActivityType

@available(iOS 7.0, *)
public static let postToTencentWeibo: UIActivityType

@available(iOS 7.0, *)
public static let airDrop: UIActivityType

@available(iOS 9.0, *)
public static let openInIBooks: UIActivityType

@available(iOS 11.0, *)
public static let markupAsPDF: UIActivityType
}

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

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")!
}

Combine UIDocumentInteractionController with UIActivityController to share on Instagram, Facebook, to mail, message and so on

Thanks to: Sharing image to Whatsapp & Facebook accepted response.

It looks that this option is actually provided out of the box for UIDocumentInteractionController, but I was presenting the controller wrongly. The correct way to display all options is to use: -presentOptionsMenuFromRect:inView:animated: instead of -presentOpenInMenuFromRect:inView:animated:
The UIDocumentInteractionController does not allow to omit some of the options like UIActivityViewControllerdoes, but I need to have Instagram alongside the other options, so this is the way to go.
The code I use:

NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"MyImage.png"];
NSData *imageData=UIImagePNGRepresentation(imageToUse);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
docIntController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
docIntController.delegate = self;
[docIntController presentOptionsMenuFromRect:CGRectMake(1, 1, 1, 1) inView:self.view animated:YES];


Related Topics



Leave a reply



Submit