Override UIActivityViewController default behaviour

痴心易碎 提交于 2019-12-10 03:08:54

问题


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


来源:https://stackoverflow.com/questions/12769499/override-uiactivityviewcontroller-default-behaviour

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