NSSharingService发送电子邮件和阅读电子邮件正文 [英] NSSharingService to send email and read email body

查看:193
本文介绍了NSSharingService发送电子邮件和阅读电子邮件正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSSharingService在Mail应用程序中打开一个电子邮件撰写窗口:

I am using NSSharingService to bring up an email compose window in the Mail app:

NSSharingService* sharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
[sharingService setRecipients:@[@"test@blahblah.com"]];
[sharingService setSubject:@"Test"];
sharingService.delegate = self;

它很好地显示了撰写电子邮件窗口,当我输入一些文本并实际发送电子邮件时,我什至收到回调给委托人的声音:

It brings up the compose email window fine, and when I enter in some text and actually send the email, I even get a callback to the delegate:

- (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items {
    NSLog(@"sharingService didShareItems - %@", sharingService.messageBody);
}

问题在于它返回的messageBody始终为nil.我期望它包含已发送电子邮件的文本.我检查了NSSharingService的头文件:

The problem is that the messageBody it returns is always nil. I was expecting this to contain the text of the email that was sent. I checked the header file for NSSharingService:

/* These read-only properties allow for querying of the shared content:
 */
// Message body as string
@property (readonly, copy) NSString *messageBody NS_AVAILABLE_MAC(10_9);
// URL to access the post on Facebook, Twitter, Sina Weibo, etc. (also known as permalink)
@property (readonly, copy) NSURL *permanentLink NS_AVAILABLE_MAC(10_9);
// Account name used for sending on Twitter or Sina Weibo
@property (readonly, copy) NSString *accountName NS_AVAILABLE_MAC(10_9);
// NSArray of NSURL objects representing the file that were shared
@property (readonly, copy) NSArray *attachmentFileURLs NS_AVAILABLE_MAC(10_9);

有人知道为什么这可能行不通吗?如果我使用NSSharingServiceNameComposeMessage而不是电子邮件,这似乎很好,但这是用于发送iMessage的.

Any idea why this might not be working? It seems to be work fine if I use NSSharingServiceNameComposeMessage instead of email, but that is for sending iMessages instead.

推荐答案

不确定您的情况为何不起作用,但这应该或多或少是遵循的标准模式:

Not sure why it is not working in your case, but this should be more or less the standard pattern to follow:

@interface sharingServiceDelegate : NSObject <NSSharingServiceDelegate>
@end

@interface sharingServiceDelegate ()
@property NSString *recipientString;
@property NSString *subjectString;
@property NSString *bodyString;
@property NSURL <NSPasteboardWriting> *attachmentURL;
@property NSSharingService *emailSharingService;
@end


@implementation sharingServiceDelegate

- (id)init {

    self = [super init];

    if (self) {
        NSSharingService *emailSharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
        emailSharingService.delegate = self;
        self.emailSharingService = emailSharingService;
    }

    return self;
}

- (BOOL)shareUsingEmail
{
    NSArray *shareItems =@[_bodyString, _attachmentURL];
    self.emailSharingService.recipients = @[_recipientString];
    self.emailSharingService.subject = _subjectString;

    if([self.emailSharingService canPerformWithItems:shareItems]){
        [self.emailSharingService performWithItems:shareItems];
        return TRUE;
    } else {
        // handle failure ...
    }

    return FALSE;
}

@end

然后,您实际上在任何需要执行的地方都执行这样的共享服务:

Then you actually perform the sharing service like this wherever you need to:

sharingServiceDelegate * shareDelegate = [[sharingServiceDelegate alloc] init];

shareDelegate.recipientString = @"recipient@address.com";
shareDelegate.subjectString = @"a subject";
shareDelegate.bodyString = @"A message body";
shareDelegate.attachmentURL = [NSURL fileURLWithPath:[NSString stringWithCString:file_to_share_path encoding:NSUTF8StringEncoding]];

if([shareDelegate shareUsingEmail]==FALSE)
    NSLog(@"Email Sharing Service failed.\n");
}

这篇关于NSSharingService发送电子邮件和阅读电子邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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