gpt4 book ai didi

macos - NSSharingService 用于发送电子邮件并读取电子邮件正文

转载 作者:行者123 更新时间:2023-12-03 17:51:30 32 4
gpt4 key购买 nike

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

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

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

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

问题是它返回的messageBody总是nil。我期望它包含已发送的电子邮件的文本。我检查了 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。

最佳答案

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

@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

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

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");
}

关于macos - NSSharingService 用于发送电子邮件并读取电子邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25414974/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com