Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a link in an email using UIActivityViewController

How do I send an email using UIActivityViewController with a quick message and a link? I have tried using an NSURL:

NSURL *url = [NSURL URLWithString:@"http://google.com/"];

And an NSString:

// String with Hyperlink syntax
NSString *stringURLHyperlink = @"<a href=\"http://google.com/">Google</a>";

// Plain text link out of desperation.
NSString *stringURLPlain = @"http://google.com/"; 

I am not using a custom UIActivity. The NSString/NSURL are being sent as activityItems:

NSArray *activityItems = @[link];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

Thanks everyone.

EDIT 1:

My apologies, I should have made my question clearer. How can I send a message and a clickable link in an email using UIActivityViewController?

like image 281
Chris Avatar asked Oct 18 '13 23:10

Chris


2 Answers

This seems to work fine in iOS 7:

[[UIActivityViewController alloc] initWithActivityItems:@[@"<html><body><b>This is a bold string</b><br\\>Check out this amazing site: <a href='http://apple.com'>Apple</a></body></html>"] applicationActivities:nil];

If you share an object that conforms to the UIActivityItemSource protocol, then you will be able to specify what the email's subject should be too!

tl;dr: Use basic html in an NSString to wrap the desired email contents

like image 66
ccjensen Avatar answered Nov 05 '22 08:11

ccjensen


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.

like image 2
Vizllx Avatar answered Nov 05 '22 09:11

Vizllx