Override UIActivityViewController default behaviou

2019-04-22 05:26发布

问题:

In the Photos app on the iPhone, when you select the Mail sharing option, the photo animates into the modal view controller that slides up. How is it possible to modify the behaviour of the built-in UIActivities? For example, I'd like to be able to set the subject field of the mail composer.

回答1:

Unfortunately, customizing the subject field of the UIActivityViewController mail composer is not working yet.

There is a documented and reported bug regarding trying to customize this discussed here:

iphone - How do I set recipients for UIActivityViewController in iOS 6?


If this were working, according to the documentation, you would be able to customize these mail composer fields:

UIActivityTypeMail: The object posts the provided content to a new email message. When using this service, you can provide NSString and UIImage objects and NSURL objects pointing to local files as data for the activity items. You may also specify NSURL objects whose contents use the mailto scheme.

So using the mailto scheme, when it is working, you should be able to customize those fields like this:

    NSString *text = @"My mail text";
    NSURL *recipients = [NSURL URLWithString:@"mailto:foo@bar.com?subject=Here-is-a-Subject"];
    NSArray *activityItems = @[text, recipients];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityController animated:YES completion:nil];

If your looking for other ways to customize the UIActivityViewControllerthere is an excellent example project here:

https://github.com/russj/ios6ShareDemo



回答2:

This is how I did it and it's working for me in iOS 7.

Create a class that conforms to the UIActivityItemSource protocol:

@interface CustomActivityItem : NSObject <UIActivityItemSource>
@end

In the implementation override the relevant methods:

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return @"";
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
{
    if ([activityType isEqualToString:UIActivityTypeMail])
    {
        return @"Subject"
    }

    return nil;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
    if ([activityType isEqualToString:UIActivityTypeMail])
    {
        return @"body";
    }

    return nil;
}

Then present the activity view controller:

CustomActivityItem* activityItem = [[CustomActivityItem alloc] init];
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[activityItem]
                                                                                       applicationActivities:nil];

[self presentViewController:activityViewController animated:YES completion:nil];