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
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.
- (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];
}
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);
}];
}
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