Send a link in an email using UIActivityViewController

10,116

Solution 1

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

Solution 2

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.

Share:
10,116
Chris
Author by

Chris

A software enthusiast and musical recording &amp; mixing engineer who questions everything.

Updated on June 17, 2022

Comments

  • Chris
    Chris almost 2 years

    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?

  • Chris
    Chris over 10 years
    Thanks for your response. I tried out the above code and your suggestions in general but the link still appears as plain text. I am trying to put a clickable link in the email. Any ideas?
  • Chris
    Chris over 10 years
    Awesome, this worked out great for me. It should be mentioned that when I used this method, other strings in the activity items array were ignored. I have not tested other objects.
  • nivritgupta
    nivritgupta over 8 years
    these HTML tags working with mfmailcomposeviewcontroller only , when you trying to share something on twitter and facebook all the tags will be shown