UIActivityViewController - Email and Twitter sharing

别来无恙 提交于 2019-11-28 03:28:43

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:

esp

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

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:

  1. Never need to integrate with more than one or two third party services.
  2. Never need to tweak the UI for the activity sheet and sharing screens.
  3. Never care to provide separate, media-specific content for each sharing type (email versus SMS, etc.)
  4. Never need to have multiple items such as a Copy Text versus a Copy Link in the same sheet.
  5. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!