I recently started working with UIActivity to share my app to the world, but I have few problems. First, I didn't find how to set the subject of my email. Is there any way? Second, when I set the body text of my email, there is a extra "enter" (first line of the email is blank and my text starts at the second line). Here's the code:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Test", nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:array applicationActivities:nil];
And in the email, it shows that:
"
Test "
Third: is there a way to know which sharing method has been selected? Because I want to include a hashtag in my post when the user shares on twitter, but now it gets integrated in the email also, which obviously doesn't make sense.
Thanks!
For adding subject to the email using UIActivityViewController on iOS6, this is the best solution that anyone can use.. All you have to do is call the following while initializing UIActivityViewController.
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
[activityViewController setValue:@"My Subject Text" forKey:@"subject"];
And your UIActivityViewController is populated with a subject.
In iOS7, this is possible by using -
activityViewController:subjectForActivityType:
When posting an item the service may provide for a separate subject field and data field, such as an email message. Implement this method if you wish to provide a subject field for services that support one.
1 and 2: How do I set recipients for UIActivityViewController in iOS 6?
Although both provided methods are a bit of a hack, especially the first one, it is possible.
3: it is possible to share different content on different services, but the number of items and their types should be the same (but it is not a limitation, really, as you can return nil for items you don't need on particular service). You have to create sharing items after the service was selected using UIActivityItemSource protocol
Code I use:
Show UIActivityViewController with the current controller as provider of all items (it should have in .h file):
const int numberOfSharedItems = 5;
- (IBAction)shareAction:(id)sender
{
NSMutableArray *shareItems = [NSMutableArray new];
while ([shareItems count] < numberOfSharedItems)
[shareItems addObject: self];
UIActivityViewController *shareController =
[[UIActivityViewController alloc]
// actual items are prepared by UIActivityItemSource protocol methods below
initWithActivityItems: shareItems
applicationActivities :nil];
shareController.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[self presentViewController: shareController animated: YES completion: nil];
}
Make placeholders for items that will be shared:
-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
static UIActivityViewController *shareController;
static int itemNo;
if (shareController == activityViewController && itemNo < numberOfSharedItems - 1)
itemNo++;
else {
itemNo = 0;
shareController = activityViewController;
}
switch (itemNo) {
case 0: return @""; // intro in email
case 1: return @""; // email text
case 2: return [NSURL new]; // link
case 3: return [UIImage new]; // picture
case 4: return @""; // extra text (via in twitter, signature in email)
default: return nil;
}
}
Make actual items that will be shared, differently for different services:
-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
// the number of item to share
static UIActivityViewController *shareController;
static int itemNo;
if (shareController == activityViewController && itemNo < numberOfSharedItems - 1)
itemNo++;
else {
itemNo = 0;
shareController = activityViewController;
}
NSString *shareText = [self shareText]; // whatever you fancy
NSURL *shareURL = [self shareURL];
// twitter
if ([activityType isEqualToString: UIActivityTypePostToTwitter])
switch (itemNo) {
case 0: return nil;
case 1: return shareText; // you can change text for twitter, I add $ to stock symbol inside shareText here, e.g. Hashtags can be added too
case 2: return shareURL;
case 3: return nil; // no picture
case 4: return @"via @YourApp";
default: return nil;
}
// email
else if ([activityType isEqualToString: UIActivityTypeMail])
switch (itemNo) {
case 0: return @"Hi!\r\n\r\nI used YourApp\r\n";
case 1: return shareText;
case 2: return shareURL;
case 3: return nil; // no picture
case 4: return [@"\r\nCheck it out.\r\n\r\nCheers\r\n" stringByAppendingString: [self userName]];
default: return nil;
}
else // Facebook or something else in the future
switch (itemNo) {
case 0: return nil;
case 1: return shareText;
case 2: return shareURL;
case 3: return [self shareImage];
case 4: return nil;
default: return nil;
}
}
u can simply create a class as follows :
@interface MYNActivityProvider : UIActivityItemProvider <UIActivityItemSource>
@end
// implementation
- (id) activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
{
if ( [activityType isEqualToString:UIActivityTypePostToTwitter] ) {
return stringToPost;
}
if ( [activityType isEqualToString:UIActivityTypePostToFacebook] ) {
return stringToPost;
}
if ( [activityType isEqualToString:UIActivityTypeMessage] ) {
return @"SMS message text";
}
if ( [activityType isEqualToString:UIActivityTypeMail] ) {
return @"Email text here!";
}
if ( [activityType isEqualToString:@"Custom"] ) {
return @"app custom text";
}
return nil;
}
You might want to try OvershareKit.
We are frequently asked why someone would use OvershareKit instead of UIActivityViewController (UIAVC) and UIActivity. UIAVC is great for apps that know they’ll never have a need for any of the following:
- Never need to integrate with more than one or two third party services.
- Never need to tweak the UI for the activity sheet and sharing screens.
- Never care to provide separate, media-specific content for each sharing type (email versus SMS, etc.)
- Never need to have multiple items such as a Copy Text versus a Copy Link in the same sheet.
- Don't mind that all non-system-provided activities get stuck with boring monochromatic icons.
Your situation is (3) - a need to care about different content for different sharing type.
来源:https://stackoverflow.com/questions/12984403/uiactivityviewcontroller-email-and-twitter-sharing