UIActivityViewController - Email and Twitter sharing UIActivityViewController - Email and Twitter sharing ios ios

UIActivityViewController - Email and Twitter sharing


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.

Check - https://developer.apple.com/library/ios/documentation/uikit/reference/UIActivityItemSource_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIActivityItemSource/activityViewController:subjectForActivityType:


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;        }}