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