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
Here's what I want it to look like (see Safari also) :
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.
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
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);
}];
}
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