27

So I'm trying out the new UIActivityViewController 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 UIActivityViewController? 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

UPDATE :

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

Ovi Tisler
  • 6,425
  • 3
  • 38
  • 63

4 Answers4

50

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.

camilomq
  • 3,021
  • 1
  • 17
  • 10
  • Spent HOURS !!! on this bloody thing, trying to figure out why it wouldn't take my url properly. Thanks man ! – Alex Dec 13 '13 at 04:16
  • Works when utrying with google.com, but when using a link to our facebook page, it just shows a ugly compass. But when I share the link on facebook, the link is an integrated "facebook page"-link which is very nice. But its very annoying that the thumbnail on the phone wont work when using in Facebooks own pages. grrr – bobmoff Mar 13 '14 at 14:45
  • FYI: subclassing `UIActivity` lets you tune what is displayed – kambala Mar 09 '15 at 16:24
  • @baquiax it works on ios 8.1. One possible reason it does not work is that your webpage did not set `og` parameter. This `og` metadata tag allows facebook to grab the specified image – MK Yung Jun 25 '15 at 15:54
  • @MKYung Thanks. I don't found a solution for it until today, but I will try again. :) – baquiax Jun 25 '15 at 17:36
2

Updated the answer for Swift 3.0

let url = URL(string: "google.com")
let shareText = "Your string goes here"
let shareItems: [Any] = [shareText, url!]

let activityVC = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityVC.excludedActivityTypes = [.airDrop, .postToFlickr, .assignToContact, .openInIBooks]

self.present(activityVC, animated: true, completion: nil)

Facebook won't accept the string, but, like many other of the social media sites, will show an image/link box for the URL

mauriii
  • 486
  • 2
  • 6
  • 17
1

If you don't specifically need to use UIActivityViewController using SLComposeViewController is easy to use for this purpose.

+(void)presentShareWidgetWithText:(NSString*)text url:(NSURL*)url images:(NSArray*)images toService:(NSString*)service presentIn:(UIViewController*)parentViewController {
    SLComposeViewController* controller = [SLComposeViewController composeViewControllerForServiceType:service];
    if (controller == nil) {
        DLog(@"Could not initialize SLComposeViewController for %@!", service);
        return;
    }
    if (url != nil) {
        if (![controller addURL:url])
            DLog(@"Failed adding url %@!", url);
    }
    if (images != nil) {
        for (UIImage* image in images) {
            if (![controller addImage:image]) {
                DLog(@"Failed adding image: %@", image);
            }
        }
    }
    if (![controller setInitialText:text]) {
        DLog(@"Failed setting initial text: %@", text);
    }

    SLComposeViewControllerCompletionHandler handler = ^(SLComposeViewControllerResult result) {
        switch (result) {
            case SLComposeViewControllerResultCancelled:
                DLog(@"SLComposeViewControllerResultCancelled");
                break;

            case SLComposeViewControllerResultDone:
                DLog(@"SLComposeViewControllerResultDone, Shared on service: %@", service);
                break;

            default:
                DLog(@"Unhandled result: %d", result);
                break;
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [parentViewController dismissViewControllerAnimated:YES completion:^{
                DLog(@"Dismissed a controller here!");
            }];
        });
    };

    [controller setCompletionHandler:handler];

    [parentViewController presentViewController:controller animated:YES completion:^{
        DLog(@"Presented %@!", controller);
    }];
}
Jonny
  • 15,955
  • 18
  • 111
  • 232
  • 1
    That doesn't work either. It adds the image in the thumbnail preview, but if you look at it, it is actually going to put the image into an album called 'iOS photos'. I want it to just be the thumbnail preview (like safari does it). I don't want the picture to go into the user's photo album – Ovi Tisler Oct 01 '12 at 17:53
  • Hmm ok I really just only used the link so far, with no image. With only the link it selects an image from the link itself. It works where the link target is an iOS app URL, which is the only thing I used so far. – Jonny Oct 02 '12 at 07:36
0
- (void)tweetURL:(NSString *)url title:(NSString *)title {
    TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
    NSString *format = @"“%@” %@ /via @DesignSceneApp";
    NSString *message = [NSString stringWithFormat:format, title, url]
    NSUInteger idx = title.length;
    while (![twitter setInitialText:message]) {
        idx -= 5;
        if (idx > 5) {
            message = [NSString stringWithFormat:format,
                [NSString stringWithFormat:@"%@…", [title substringToIndex:idx]],
                url
            ];
        } else {
            // Give up on the title.
            message = [NSString stringWithFormat:@"%@ /via @DesignSceneApp", url];
            [twitter setInitialText:message];
            break;
        }
    }

    [self presentViewController:twitter animated:YES completion:nil];
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211