Macos NSSharingService发送电子邮件和读取电子邮件正文
我正在使用NsharingService在Mail应用程序中打开电子邮件撰写窗口:Macos NSSharingService发送电子邮件和读取电子邮件正文,macos,cocoa,osx-mavericks,appkit,Macos,Cocoa,Osx Mavericks,Appkit,我正在使用NsharingService在Mail应用程序中打开电子邮件撰写窗口: NSSharingService* sharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail]; [sharingService setRecipients:@[@"test@blahblah.com"]]; [sharingService setSubject:@"Test"]; sharing
NSSharingService* sharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
[sharingService setRecipients:@[@"test@blahblah.com"]];
[sharingService setSubject:@"Test"];
sharingService.delegate = self;
它会很好地打开“撰写电子邮件”窗口,当我输入一些文本并实际发送电子邮件时,我甚至会收到一个对代理的回调:
- (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items {
NSLog(@"sharingService didShareItems - %@", sharingService.messageBody);
}
问题是它返回的messageBody始终为零。我希望这包含发送的电子邮件的文本。我检查了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
而不是电子邮件,似乎效果不错,但那是用来发送iMessages的 不确定为什么它在您的案例中不起作用,但这或多或少应该是要遵循的标准模式:
@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
我不想预先填充电子邮件内容并打开撰写窗口(我可以做得很好)。我指的是在电子邮件实际发送后访问“messageBody”属性,它在头文件中似乎标记为“readonly”,但在消息发送后总是返回nil。
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");
}