Sending Custom Strings to UIActivityTypes (whhhhaaaaaaaaa)

You want to send different messages to twitter, facebook, and email via your app's share sheet options but how?

Well the secret is a custom delegate of UIActivityItemProvider. 

UIActivityItemProvider acts as a proxy for data to get to an active view controller. It is often used when you know what you want to send but it isn't ready yet, like sending a video to Youtube. The placeholder you create when alloc/init'ing the UIActivityItemProvider will be empty until the real object is ready which will then be used in replacement.

So what if you want to send different text to the different share options...

First create a custom subclass of UIActivityItemProvider:

.h

//
//  CustomItemUIActivityItemProvider.h
//  UIActivityItemProviderStuff
//
//  Created by Ian Smith on 4/9/15.
//  Copyright (c) 2015 Poop. All rights reserved.
//

#import <UIKit/UIKit.h>

@class FISDonorsChooseProposal;

@interface CustomItemUIActivityItemProvider : UIActivityItemProvider

-(id)initWithProposal:(Proposal *)proposal andPlaceholder:(NSString *)text;

@property (strong, nonatomic) Proposal *proposal;

@end

In the .m we need a customer initializer and to override the 'item' method that defines the text that is going to go to the different share options a user can choose. The sharing options are populated by 

//
//  CustomItemUIActivityItemProvider.m
//  
//
//  Created by Ian Smith on 4/9/15.
//  Copyright (c) 2015 Poop. All rights reserved.
//

#import "CustomItemUIActivityItemProvider.h"
#import "Proposal.h"

@implementation CustomItemUIActivityItemProvider

-(id)initWithProposal:(Proposal *)proposal andPlaceholder:(NSString *)text {
    self = [super initWithPlaceholderItem:text];
    
    if (self) {
        _proposal = proposal;
    }
    return self;
}


-(id)item
{
    NSString *formattedURL = [self.proposal.proposalURL componentsSeparatedByString:@"?"][0];
    NSString *shareText = [NSString stringWithFormat:@"This is the standard text string"];

    if (self.activityType == UIActivityTypeMail ) {
        return [NSString stringWithFormat:@"%@", shareText];


    } else if (self.activityType == UIActivityTypePostToTwitter) {
        NSString *tweetText = [NSString stringWithFormat:@"Check out my project %@ - %@", self.proposal.title, formattedURL];
        return [NSString stringWithFormat:@"%@", tweetText];
   

} else if (self.activityType == UIActivityTypeMessage)  {

       NSString *imessageText = [NSString stringWithFormat:@"Check out my project %@ - %@.\n &@", self.proposal.title, formattedURL,self.proposal.formattedText];
        return [NSString stringWithFormat:@"%@", imessageText];


    } else if { (self.activityType == UIActivityTypePostToFacebook) {

NSString *fbText = [NSString stringWithFormat:@"Hey Facebook friends, %@ \n\n\n %@", self.proposal.facebookText, formattedURL,];
        return [NSString stringWithFormat:@"%@", fbText];
}

@end

And to implement on the view controller --->

Add <UIActivityItemSource> as a delegate to your VC

And create an action -

- (void)shareTapped {
    CustomItemUIActivityItemProvider *customItem = [[CustomItemUIActivityItemProvider alloc] initWithProposal:self.proposal andPlaceholder:@"Placeholder Text"];


    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[customItem] applicationActivities:nil];


    [self presentViewController:activityVC animated:YES completion:nil];
}

And with that for each selection that a user makes when sharing from your app the text they share will be different.