NSSharingService to send email and read email body

会有一股神秘感。 提交于 2019-12-02 10:56:01

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");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!