Override UIActivityViewController default behaviour
Asked Answered
L

2

9

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.

Labarbera answered 7/10, 2012 at 14:5 Comment(0)
U
15

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:[email protected]?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

Unintelligible answered 17/10, 2012 at 15:38 Comment(1)
Thanks for the link. For Mail, or anything else that doesn't behave like you want it to, I believe it's possible to create a custom UIActivitySource that does what you want. In this case, exclude UIActivityMail, and create a custom activity that fills in the subject field etc.Mope
R
4

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];
Rhizobium answered 12/2, 2014 at 16:45 Comment(3)
It does not appear this approach works for iOS9. You will get an error 'init' has been explicitly marked unavailable here.Lactoprotein
I'm still using this approach in iOS 9, compiling with Xcode Version 7.0.1 (7A1001), and it works fine. What's the exact error you're getting?Rhizobium
Does this add a recipient?Xeniaxeno

© 2022 - 2024 — McMap. All rights reserved.