首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于发送电子邮件和读取电子邮件正文的NSSharingService

用于发送电子邮件和读取电子邮件正文的NSSharingService
EN

Stack Overflow用户
提问于 2014-08-21 05:48:13
回答 1查看 3.2K关注 0票数 2

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

代码语言:javascript
复制
NSSharingService* sharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
[sharingService setRecipients:@[@"test@blahblah.com"]];
[sharingService setSubject:@"Test"];
sharingService.delegate = self;

它可以很好地打开撰写电子邮件窗口,当我输入一些文本并实际发送电子邮件时,我甚至会收到对代表的回调:

代码语言:javascript
复制
- (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items {
    NSLog(@"sharingService didShareItems - %@", sharingService.messageBody);
}

问题是它返回的messageBody总是为零。我预计这将包含发送的电子邮件的文本。我检查了NSSharingService的头文件:

代码语言:javascript
复制
/* 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代替电子邮件似乎没什么问题,但那是用来发送iMessages的。

EN

回答 1

Stack Overflow用户

发布于 2014-12-09 18:30:37

不确定为什么它在您的案例中不起作用,但这应该或多或少是要遵循的标准模式:

代码语言:javascript
复制
@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

然后,您可以在需要的任何地方像这样执行共享服务:

代码语言:javascript
复制
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");
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25414974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档