Give Thumbnail Image with Uiactivityviewcontroller

Give thumbnail image with UIActivityViewController

In order to share two different set of content you have to create two different itemsource

  1. we can set different text content for different activity type.Add the MyStringItemSource class to your viewcontroller

    SourceOne:

    class MyStringItemSource: NSObject, UIActivityItemSource {

    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
    return ""
    }

    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
    //You can pass different text for for diffrent activity type
    if activityType == UIActivityTypePostToFacebook {
    return "String for facebook"
    }else{
    return "String for Other"
    }
    }
    }
  2. Our requirement is to add image to all activity type except FB,to do that add the MyImageItemSource class in your VC.

    SourceTwo:

    class MyImageItemSource: NSObject, UIActivityItemSource {

    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
    return ""
    }

    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
    //This one allows us to share image ecxept UIActivityTypePostToFacebook
    if activityType == UIActivityTypePostToFacebook {
    return nil
    }
    let Image: UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string: "https://pbs.twimg.com/profile_images/604644048/sign051.gif")!)!)!
    return Image
    }

    }
  3. Now we are ready to set UIActivityViewController,here we go

    @IBAction func Test(sender: AnyObject) {

    let activityVC = UIActivityViewController(activityItems: [MyStringItemSource(),MyImageItemSource()] as [AnyObject], applicationActivities: nil)

    //Instead of using rootviewcontroller go with your own way.
    if let window = (UIApplication.sharedApplication().delegate as? AppDelegate)?.window
    {
    window.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
    }
    }

TWITTER Shared Dialogue:
Contains image and given text

Sample Image

FB Share Dialogue:
Contains only the given text

Sample Image

iOS13 share sheet: how to set preview thumbnail when sharing UIImage

Just pass the image urls to UIActivityViewController not the UIImage objects.
For example:

let imageURLs: [URL] = self.prepareImageURLs()
let activityViewController = UIActivityViewController(activityItems: imageURLs, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)

You can see that the image name and the image properties are shown in the top of the UIActivityViewController. Hope it helps!

iOS 13, Custom Image, title and subtitle in the presented UIActivityViewController

I have found a solution using UIActivityItemSource

UIActivityItemSource have this protocol activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata?which we can use to set image title and subtitle for our UIActivityViewController

This is an example:

 public func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()

metadata.title = "My title" // Preview Title

// Set image

metadata.imageProvider = NSItemProvider(object: image)
metadata.iconProvider = NSItemProvider(object: image)
metadata.url = urlImage

// Set URL for sharing
metadata.originalURL = myUrl // Add this if you want to have a url in your share message.

return metadata
}

And this is the result: I have my custom image and title.

Sample Image

How do I customize a UIActivityViewController to show a URL link when posting to facebook and twitter?

I've searched about this and I've come to the conclusion that using the UIActivityViewController there's just no way to do what you intend to. Every time you try to share a photo it just makes you put the photo in one of your albums, "iOS" album by default.

In my case, I did not need to post a photo as long as an image was retrieved from the link I needed to share (just like when you post a link on your Facebook timeline and it automatically grabs a picture from that link, and the picture becomes the link). In order to do that, I figured out that if I posted only an URL it does not work. You have to post some initial text and then the URL, and you do so by putting the objects in that order within the activityItems array.

This is my working code:

NSArray * activityItems = @[[NSString stringWithFormat:@"Some initial text."], [NSURL URLWithString:@"http://www.google.com"]];
NSArray * applicationActivities = nil;
NSArray * excludeActivities = @[UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeMessage];

UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityController.excludedActivityTypes = excludeActivities;

[self presentViewController:activityController animated:YES completion:nil];

I also noticed that if you post a photo along these objects, it will not retrieve any image from the link.

I hope this helps.

Set different activity items for UIActivityViewController Swift

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



Related Topics



Leave a reply



Submit