简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM