Ovi Tisler Ovi Tisler - 6 months ago 60
iOS Question

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

So I'm trying out the new

in iOS 6, and it's really easy to get up and running, but I can't figure out how to control it like I want. So I want to post a link to an article and I also want to attach an image for the link. I DON'T want that image uploaded to some facebook album, just as a URL thumbnail.

This is easy to accomplish in the facebook SDK as they give full control for it, but is there a way to do it with the
? Here's what I got:

NSArray *activityItems = @[[NSURL URLWithString:[article link]], [UIImage imageNamed:@"myStockImage"]];
UIActivityViewController *viewCont = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:viewCont animated:YES completion:nil];

And this works, but it uploads the image to an 'iOS album'. If I don't add the image in the array, then the facebook sheet looks like it's blank and the attachment shows a grayed out safari logo (like the thumbnail is missing!) In safari when you try to Facebook a link, it uses a screen grab of the page as the thumbnail, I want to do something like that


So here's what it looks like from my app (using UIActivityViewController or the SLCompose way). See how it is going to upload the picture to a photo album
iOS Photos

Incorrect placement of photo

Here's what I want it to look like (see Safari also) :

enter image description here

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download