0

I am trying to send an email(open the default mail client application) from my FinderSync extension.

        let service = NSSharingService(named: NSSharingService.Name.composeEmail)
        if(service != nil)
        {
            service!.recipients = ["test@gmail.com"]
            service!.subject = "Test Mail"
           if service!.canPerform(withItems: ["Test Mail body"])
           {
                service!.perform(withItems: ["Test Mail body"])
           }
           else
           {
                  //fail for me
          }
        }

The same code executes from AppDelegate/ViewController while starting the main application but not working when calling from the extension.

Marek H
  • 5,173
  • 3
  • 31
  • 42
DAC84
  • 421
  • 1
  • 8
  • 20
  • 1
    What is the result of canPerform? I am afraid NSExtension instances are not allowed for NSSharingService (this is from dissasembly ShareKit.framework) – Marek H Aug 31 '21 at 13:28
  • canPerform returns failure. I do not know how to check error message. I tried using delegate which also does not gets called – DAC84 Aug 31 '21 at 13:36
  • Read the answer – Marek H Aug 31 '21 at 13:36

1 Answers1

1

FinderSync is an NSExtension which is not allowed to use NSSharingService

/* @class SHKSharingService (ShareKit.framework) */  

-(char)canPerformWithItems:(NSArray *)items {
    if (([[SHKSharingService class] isShareKitPlugInService] || ([items count] > 0x1388)) {
    return NO;
}

isShareKitPlugInService is defined as

[[NSBundle mainBundle] infoDictionary][@"NSExtension"]
Marek H
  • 5,173
  • 3
  • 31
  • 42
  • thank you so much. Is there any other way to initiate email from Extension. I tried using "mailto" option. But there is no way to attach a file with "mailto" – DAC84 Aug 31 '21 at 13:40
  • 1
    I think it's better to open new question. – Marek H Aug 31 '21 at 13:40
  • 1
    One idea might be to write xpc service (it might work) – Marek H Aug 31 '21 at 13:47
  • 1
    I have done it by creating a separate application for opening mail application and call that new application from FinderSync using NSWorkspace.shared.openApplication – DAC84 Sep 20 '21 at 10:47