Send a link in an email using UIActivityViewController - ios

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 = #"Google";
// 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.

Related

How to share Deeplinking url via Email

I tried to share Url, for instance,deeplink:// via default iphone Email.But unfortunately,the link doesn't turn as hyperlink mode so that it can be clickable and directed to the specified location.So please kindly help me for the below code.
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"deeplink://"]];
NSArray * activityItems = #[url];
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];
Custom URI schemes are not a valid solution for this. As you discovered, they are not recognized as links in many situations, but there are other more serious issues too. Read this answer for more information

Show all options in UIActivityViewController

I have some trouble with create native share dialog.
My goal is share link of my product by native view controller, but I can't do it. I'm trying test code:
NSURL *google = [NSURL URLWithString:#"http://google.com"];
NSArray *activityItems = #[google];
UIActivity *activity = [UIActivity new];
UIActivityViewController *activityViewControntroller = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:#[activity]];
activityViewControntroller.excludedActivityTypes = #[];
[self presentViewController:activityViewControntroller animated:true completion:nil];
And I get no FB and twitter icons. In the other hand, I can see it in safari both icons and when I'm touching it I see "You need configure your account before post".
So, I want the same behavior: icons for all most-popular social networks such as VK, FB, twitter; messengers like whatsapp, viber and telegram. If some of them is not installed I want dialog "You have to log in before" and redirect to safari (e.g. for FB) to log in or open appstore (e.g. for telegram or viber) to install; otherwise I want post message with my link.
I see it like this: exclude all types from activity vc, than forcibly add all icons I need and check each of them on click manually. But in apple docs I've read that "you have to use native behavior for native items instead of create custom". So, will it be correct? Or there are other ways to solve this?
Try this
-(void)clickShare:(UIButton *)sender{
NSArray * activityItems = #[[NSString stringWithFormat:#"Some initial text."], [NSURL URLWithString:#"http://www.google.com"]];
NSArray * applicationActivities = nil;
NSArray * excludeActivities = #[];
UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityController.excludedActivityTypes = excludeActivities;
[self presentViewController:activityController animated:YES completion:nil];
}
this code working in my project.

How to pass API and fetched data to UIActivityViewCollector

I am using a table view and i am using it to display clips which i am fetching from core data. Now i have an API for clip path which will connect it to the server clips i want to pass clip id with that API and pass the clip name fetched from the core data.i have tried following thing to make it work but gives me error.I think i am doing it wrong. How would i do that.
//here is my code but it is giving me error. Can't pass it this way.
NSArray * activityItems = #[[NSURL URLWithString:#"http://example.com/view-videos.html?task=clip.details&id=%#",_fetchid]]; // this is giving me error and here i also want to send selected clip name with this API
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:videoToShare applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePostToFacebook,UIActivityTypePostToTwitter,UIActivityTypePostToWeibo,UIActivityTypeCopyToPasteboard,UIActivityTypeMessage];
[self presentViewController:activityVC animated:YES completion:nil];
}
Try something like this. You are passing some other object not activityItems.
NSString *urlString = [NSString stringWithFormat:#"http://example.com/view-videos.html?task=clip.details&id=%#",fetchid];
NSArray * activityItems = #[[NSURL URLWithString:urlString]];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePostToFacebook,UIActivityTypePostToTwitter,UIActivityTypePostToWeibo,UIActivityTypeCopyToPasteboard,UIActivityTypeMessage];
[self presentViewController:activityVC animated:YES completion:nil];
If you want to share some video then also add that video in activityItems array object
Hope this will help you.

UIActivityViewController Automatically 'Clicking' Link iOS 7

I have a basic UIActivityViewController sharing some text and a URL. The sharing options are Email, SMS, Twitter, and Facebook. Everything works fine in iOS 6 but in iOS 7 when I choose Twitter or Facebook, without touching anything my device automatically closes my app, opens Safari and goes to the URL I am trying to share. What could be causing this?
Code (CustomActivityItemProvider is a subclass to customize text based on sharer):
if ([UIActivityViewController class]) { // if class exists = ios6+
// Create array for sharing
CustomActivityItemProvider *textToShare = [[CustomActivityItemProvider alloc]
initWithStandardText:#"Text to share.\n"];
NSURL *urlToShare = iTunesShort ? iTunesShort : [NSURL URLWithString:iTunesLink];
NSArray *activityItems = #[textToShare, urlToShare];
// Show sharing view
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
So I still don't know why only shortened URLs were causing this problem, but a solution that took care of it was to include the urlToShare in the textToShare.
// Create array for sharing
NSString *urlToShare = iTunesShort ? iTunesShort : iTunesLink;
NSString *textWithUrl = [NSString stringWithFormat:#"Text to share.\n%#", urlToShare];
CustomActivityItemProvider *textToShare = [[CustomActivityItemProvider alloc]
initWithStandardText:textWithUrl];
NSArray *activityItems = #[textToShare];

iOS6 UIActivity tweetsheet text not populating

I'm using UIActivity class for the first time, with very little modification:
NSString *textToShare = #"my text";
UIImage *imageToShare = [UIImage imageNamed:#"myImage.png"];
NSArray *itemsToShare = #[textToShare, imageToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:YES completion:nil];
When I test this, emails, messages, and Facebook are automatically populated with both the textToShare and the imageToShare, while only the image appears on the tweet sheet.
Apple UIActivity class documentation says UIActivityTypePostToTwitter will accept NSString, NSAttributedString, UIImage, AVAsset, and NSURL objects.
The WWDC video examples largely feature Facebook examples, and I've never dealt with Twitter before.
I thought it would populate just like the email/message/Facebook did, and I'm not sure where to go from here. Seems like it should be really simple, but my searches result in much more complex custom-Twitter problems and solutions.
"my text" was a few characters over the 140 character limit of a tweet. Shortened the text, tweet sheet populates.

Resources