如何在iOS 6中为UIActivityViewController设置收件人? [英] How do I set recipients for UIActivityViewController in iOS 6?

查看:88
本文介绍了如何在iOS 6中为UIActivityViewController设置收件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS6中使用新的 UIActivityViewController 类来为用户提供各种共享选项。您可以将一系列参数传递给它,例如文本,链接和图像,剩下的就完成了。

I'm using the new UIActivityViewController class in iOS6 to provide the user with various sharing options. You can pass an array of parameters to it such as text, links and images and it does the rest.

如何定义收件人?例如,通过邮件或短信共享应该能够接受收件人,但我无法弄清楚如何调用此行为。

How do I define recipients? For example sharing via mail or SMS should be able to accept recipients but I can't figure out how to invoke this behaviour.

我不想要有单独使用 MFMessageComposeViewController UIActivityViewController ,因为这会破坏共享控制器的用途。

I don't want to have to have to use MFMessageComposeViewController and UIActivityViewController separately as that just defeats the purpose of the share controller.

有任何建议吗?

UIActivityViewController类参考

编辑:现已提交Apple和随后与重复的错误报告合并。

关于OpenRadar的错误报告

推荐答案

所有这些都归功于Emanuelle,因为他提出的最多代码。

All credit here goes to Emanuelle, since he came up with most of the code.

虽然我以为我会发布他的代码的修改版本,有助于设置收件人。

Though I thought I would post a modified version of his code that helps set the to recipient.

我在MFMailComposeViewController上使用了一个类别

I used a Category on MFMailComposeViewController

#import "MFMailComposeViewController+Recipient.h"
#import <objc/message.h>

@implementation MFMailComposeViewController (Recipient)

+ (void)load {
    MethodSwizzle(self, @selector(setMessageBody:isHTML:), @selector(setMessageBodySwizzled:isHTML:));
}



static void MethodSwizzle(Class c, SEL origSEL, SEL overrideSEL)
{
    Method origMethod = class_getInstanceMethod(c, origSEL);
    Method overrideMethod = class_getInstanceMethod(c, overrideSEL);

    if (class_addMethod(c, origSEL, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
        class_replaceMethod(c, overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        method_exchangeImplementations(origMethod, overrideMethod);
    }
}

- (void)setMessageBodySwizzled:(NSString*)body isHTML:(BOOL)isHTML
{
    if (isHTML == YES) {
        NSRange range = [body rangeOfString:@"<torecipients>.*</torecipients>" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
        if (range.location != NSNotFound) {
            NSScanner *scanner = [NSScanner scannerWithString:body];
            [scanner setScanLocation:range.location+14];
            NSString *recipientsString = [NSString string];
            if ([scanner scanUpToString:@"</torecipients>" intoString:&recipientsString] == YES) {
                NSArray * recipients = [recipientsString componentsSeparatedByString:@";"];
                [self setToRecipients:recipients];
            }
            body = [body stringByReplacingCharactersInRange:range withString:@""];
        }
    }
    [self setMessageBodySwizzled:body isHTML:isHTML];
}

@end

这篇关于如何在iOS 6中为UIActivityViewController设置收件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆